在我的工作中,我必須解決一個URL重寫問題。我必須使用的URL爲模板:MVC4 Route as/{controler} - {id}而不是/ {controler}/{id}
{controller}/{action}-{id}
,而不是
{controller}/{action}/{id}
所以經加工後的URL應該是這樣的:
myController/myAction-128
,其中128是參數
我路線圖:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "NewRoute", // Route name
url: "{controller}/{action}-{id}/{extId}", // URL with parameters
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, extId = UrlParameter.Optional } // Parameter defaults
);
}
控制器代碼:
[HttpGet]
public ActionResult DocumentDetails(int? id)
{
if (doc.HasValue)
{
...
}
}
這條路線不提供任何成功的結果。我仍然有404錯誤。當我使用/而不是「 - 」時,一切正常,但我的JS視圖環境不起作用。
有什麼我可以做的,以解決這個問題?所有的幫助將不勝感激,謝謝。
謝謝,這段代碼有效。順便說一句:是@「\ d +」路由中的數字的正則表達式? – 2013-03-24 14:48:31
是的,'\ d +'是一個匹配一個或多個數字的正則表達式。 – 2013-03-24 14:51:35