url 中没有控制器的 MVC 路由
如何配置 ASP.NET MVC 3 路由,使其不在 url 中显示控制器?
这是我
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"HomeActions",
"{action}",
new { action= "AboutUs" }
);
需要网址的路线:
mysite.com/AboutUs
但我有
mysite.com/Home/AboutUs
How do I configure ASP.NET MVC 3 routing so it doesn’t shown the controller in the url?
Here's my routes
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"HomeActions",
"{action}",
new { action= "AboutUs" }
);
I need url:
mysite.com/AboutUs
But I have
mysite.com/Home/AboutUs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会非常具体地说明您要路由的网址。并将其放置在默认路由之上。
对您建议的路线不太具体可能会产生不良后果。特别是如果在默认路由下面列出。
例如,如果将上述路由添加到默认路由之后,则 url http://www.example.com/AboutUs< /a> 可能会匹配路由 {controller = "AboutUs", action = "Index", id = UrlParamter.Optional}。如果您在默认路由之上添加了路由,则查找 url http://www.example.com/Users< /a> 您可能希望作为用户控制器上的索引操作现在将在主控制器上查找用户操作。
因此,我建议对此类路线进行具体说明。
I would be very specific about the url that you want to route. And place it above the default route.
Being less specific with a route like the one you suggested might have unwanted consequences. Especially if listed below the default route.
For example, if the above route is added after the default then the url http://www.example.com/AboutUs would likely match the route {controller = "AboutUs", action = "Index", id = UrlParamter.Optional}. If you added the route above the default one, then looking for the url http://www.example.com/Users which you might want to be the Index action on the Users controller would now look for the Users action on the Home controller.
So, I would advise being specific about routes like that.
您需要添加不带
{controller}
部分的路由,并在 defaults 参数中指定控制器名称。You need to add a route without the
{controller}
portion, and specify the controller name in the defaults parameter.