ASP.NET MVC - 从 URL 中删除控制器名称 [英] ASP.NET MVC - Removing controller name from URL
本文介绍了ASP.NET MVC - 从 URL 中删除控制器名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从我的 URL 中删除控制器名称(对于一个特定的控制器).例如:
I want to remove the controller name from my URL (for one specific controller). For example:
http://mydomain.com/MyController/MyAction
我希望将此 URL 更改为:
I would want this URL to be changed to:
http://mydomain.com/MyAction
我将如何在 MVC 中执行此操作?如果这对我有帮助,我正在使用 MVC2.
How would I go about doing this in MVC? I am using MVC2 if that helps me in anyway.
推荐答案
你应该在 global.asax 中映射新的路由(添加在默认路由之前),例如:
You should map new route in the global.asax (add it before the default one), for example:
routes.MapRoute("SpecificRoute", "{action}/{id}", new {controller = "MyController", action = "Index", id = UrlParameter.Optional});
// default route
routes.MapRoute("Default", "{controller}/{action}/{id}", new {controller = "Home", action = "Index", id = UrlParameter.Optional} );
这篇关于ASP.NET MVC - 从 URL 中删除控制器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文