2016-11-23 74 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();