如何将url重写为: /SomePage to /Pages/ShowPage/SomePage?
我试过了:
routes.MapRoute("myroute", "{id}", new { controller = "Pages", action = "ShowPage" });但它不起作用。我做错了什么?
发布于 2009-11-05 08:51:03
如果你想说“导航到/SomePage应该调用PagesController.ShowPage("SomePage")",那么你可能想要这样:
// Find a method with signature PagesController.ShowPage( string param )
// and call it as PagesController.ShowPage("SomePage")
route.MapRoute(
"MyRoute",
"SomePage",
new { controller = "Pages", action = "ShowPage", param = "SomePage" } );这只会重定向确切的URL /SomePage。如果您试图说“导航到/{something}将运行PagesController.ShowPage( something )方法”,那么这是一个更困难的问题。
如果第二种情况确实是您想要的,那么您必须在大多数其他方法之后才能定义它。您需要的路由条目为:
// This will call the method PagesController.ShowPage( string param )
route.MapRoute(
"MyRoute",
"{param}",
new { controller = "Pages", action = "ShowPage" } );发布于 2009-11-05 00:23:50
我认为这应该是:
routes.MapRoute("myroute", "{controller}/{action}/{id}", new { controller = "Pages", action = "ShowPage", id = "SomePage" });发布于 2009-11-05 00:54:50
这是错误的,因为我认为在您的应用程序中,有一个默认的地图路由:
routes.MapRoute(
"root", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);它将查找名称与您传入的id相同的控制器,如果您删除此默认映射路由,您的映射路由将起作用。
你应该试试这个路由调试器工具,它有很大的帮助:
Debugger Tool
https://stackoverflow.com/questions/1674899
复制相似问题