2016-11-23 139 views
0

我有一个问题让我的模板出现在我的SPA。所有脚本执行时都没有错误,我可以通过浏览器导航到我的HTML模板,但角度无法将我的路由配置连接到我的应用程序。ASP.NET Core与AngularJS路由

(function(angular) { 
    'use strict'; 

    var app = angular.module('over_the_counter', [ 
     'ngRoute' 
    ]); 

    app.run(function() { 
    }); 

    app.config(['$routeProvider', 
     function ($routeProvider) { 
      $routeProvider. 
       when('/', { 
        templateUrl: 'app/landing/landing.html', 
        controller: 'LandingController', 
        controllerAs: 'ctrl' 
       }). 
       otherwise({ 
        redirectTo: '/' 
       }); 
     } 
    ]); 
}(window.angular)); 

如果我浏览到http://localhost:55907/app/landing/landing.html,然后呈现页面。该HTML文件位于wwwroot文件夹中。

+0

您可以用控制器源提供,你做你的连接?此代码除了角应用程序和路由的初始化之外什么也不做。 –

+0

@GrayFox为什么你需要看到控制器源?我的路由提供程序在初始化期间正在构建路由。 – bdparrish

+0

我误解了你。所以,如果我的理解正确,你的页面不能在'http:// localhost:55907 /'url? –

回答

0

你需要更多的配置到代码appsettings.json或者只是这些行添加到startup.cs文件:

DefaultFilesOptions options = new DefaultFilesOptions(); 
      options.DefaultFileNames.Clear(); 
      options.DefaultFileNames.Add("Index.html"); 
      app.Use(async (context, next) => 
      { 
       await next(); 

       if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value)) 
       { 
        context.Request.Path = "/Index.html"; 
        await next(); 
       } 
      }) 
      .UseCors("AllowAll") 
      .UseDefaultFiles(options) 
      .UseStaticFiles() 
      .UseIdentity() 
      .UseMvc();