我想为我的asp.net mvc应用程序定制URL。目前我正在开发asp.net MVC4.5
案例:1
Current URL : localhost/Country/Index/param1
Expected URL : localhost/param1 案例:2
Current URL : (1) localhost/Country/Index/param1 (Param1 is name of country)
(2) localhost/State/Index/param11 (param11 is name of state)
Expected URL : localhost/param1/param11 (I need second url like this.)提前谢谢。
发布于 2017-03-21 19:22:09
ASP.net MVC处理基于routing.The路由的请求,处理从路由表获取的数据。所以基本上你必须至少让控制器和URL为我们提供更改action.But的选项,如下所示:
routes.MapRoute(
name: "Test",
url: "myURl/{param1}",
defaults: new { controller = "myURl", action = "actualActionName", id = UrlParameter.Optional }
);在MVC5中,属性路由也可以做到这一点。
发布于 2017-08-17 17:33:37
要实现您的结果,只需为每个事件创建单独的路由器。
目前您可能正在使用如下所示的默认路由。
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);或者尝试创建一些不同的路由。
我的建议是为每个事件创建单独的路线。like :使用param1和param11创建路由,以获得期望的结果。
routes.MapRoute(
name: "Default",
url: "param1/param11",
defaults: new { controller = "param1", action = "param11", id = UrlParameter.Optional }
);为另一个事件和方法创建相同的对象。
https://stackoverflow.com/questions/42870303
复制相似问题