我想用AngularJS 2使用ASP.NET MVC(不是核心),並且有一些路由問題。Angular 2 - 使用ASP.NET MVC進行路由
首先在RouteConfig.cs,我有以下途徑定義
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
// when the user types in a link handled by client side routing to the address bar
// or refreshes the page, that triggers the server routing. The server should pass
// that onto the client, so Angular can handle the route
routes.MapRoute(
name: "spa-fallback",
url: "{*url}",
defaults: new { controller = "Home", action = "Index" }
);
在我app.route.ts(角路由),我剛纔定義了幾個路線。我的默認路由重定向到像
export const router: Routes = [{
path: '',
redirectTo: 'auctions/e231',
pathMatch: 'full'
},
{
path: 'auctions/:id',
component: AuctionComponent,
children: []
}
];
它加載角的應用和缺省路由我app.route.ts重定向我的另一條路線。當我運行應用程序,我的服務器的路由/首頁/指數送達了罰款到拍賣/ E231和我的瀏覽器的最終URL變得
http://localhost:53796/auctions/e231
一切正常,但是當我刷新頁面以這樣的URL,我得到的資源服務器錯誤未找到,這也是預料之中,因爲它會尋找名爲Auctions的控制器不存在於MVC中。我想知道爲什麼我在RouteConfig.cs中的spa-fallback路由沒有找到?在asp.net mvc中,還有更好的方法來處理這種情況,因爲我想能夠使用我的一些MVC控制器的操作,例如/ Account/Login等。
爲什麼你不使用從服務器端和客戶端的另一個路由?我的意思是讓剃鬚刀加載主視圖,並從HTML和JS讓角路由器的作品? –
使用asp.net MVC(作爲用戶界面)和angular2並不是一個好主意。 –
@HassanFalahi是的,但當時的要求是這樣的使用身份服務器,並使用內置的模板進行用戶管理 –