我有一個默認的C#MVC路線:像C#MVC路由 - 多種途徑
routes.MapRoute(
"Default",
"{controller}/{action}/{id}"
new { controller = "Home", action = "Index", id = "Welcome" }
);
現在我會網址:
mysite.com/Home/Index/Page1
mysite.com/Home/Index/Page2
mysite.com/Home/Index/Page3
mysite.com/Account/Login
mysite.com/Account/Etc
但我想有第一套具有較短網址,如:
mysite.com/Page1
mysite.com/Page2
mysite.com/Page3
mysite.com/Account/Login
mysite.com/Account/Etc
我預想的代碼是非常簡單的,如:
routes.MapRoute(
"Shorturl",
"{id}",
new { controller = "Home", action = "Index", id = "Welcome" }
);
routes.MapRoute(
"Default",
"{controller}/{action}/{id}"
new { controller = "Home", action = "Index", id = "Welcome" }
);
但這並不奏效。它只會走第一條路線,忘記第二條路線。 當只有一個參數(如 mysite.com/Page1)時,如何讓程序採用第一條路線,並在有多條路線(如mysite.com/Account/Login)時採用第二條路線?
編輯: 我可以這樣做:
routes.MapRoute("Short", "short/{id}", new { controller = "Home", action = "Indx", id = "Page1" });
但後來我將有一個醜陋的URL 「短/」。 我可以解決這個問題:
routes.MapRoute("Page1", "Page1", new { controller = "Home", action = "Index", id = "Page1" });
但後來我需要手動添加每個新頁......
http://stackoverflow.com/questions/2975642/routing-in-asp-net-mvc-2-0 – CodeCaster 2011-12-23 09:55:38