如何配置ASP.NET MVC路由隐藏在&QUOT控制器名称;家"页? [英] How do I configure ASP.NET MVC routing to hide the controller name on a "home" page?
问题描述
从这个问题继:
<一个href=\"http://stackoverflow.com/questions/1713379/asp-net-mvc-routing-with-default-controller\">ASP.NET MVC路由与默认控制器
我有一个类似的要求在我的最终用户不希望看到的URL降落或主页为他们的应用控制器名称。
I have a similar requirement where my end user doesn't want to see the controller name in the url for the landing or "home page" for their application.
我有一个名为 DeviceController
控制器,我想成为的主页的控制器。该控制器具有一些行动,我想使用的URL类似以下内容:
I have a controller called DeviceController
which I want to be the "home page" controller. This controller has a number of actions and I'd like to use URL's like the following:
http://example.com -> calls Index()
http://example.com/showdevice/1234 -> calls ShowDevice(int id)
http://example.com/showhistory/1224 -> calls ShowHistory(int id)
我还需要为这个控制器离开了URL的 /设备
部分生成的链接。
我也有一些其他的控制器,例如 BuildController
:
I also have a number of other controllers, for example BuildController
:
http://example.com/build
http://example.com/build/status/1234
http://example.com/build/restart/1234
和等。 URL的这些控制器,因为它们都是精品。
and so on. The URL's for these controllers are fine as they are.
问题是,我似乎连学习的答案,上面提到的问题后,让我的路由此头左右。
The problem is that I just can't seem to get my head around the routing for this even after studying the answers to the question referenced above.
有人可以提供一个code样本解释如何做到这一点?
Can someone provide a code sample explaining how to do this?
我使用ASP.NET MVC2。
I'm using ASP.NET MVC2.
推荐答案
试试这个:
private void RegisterRoutes(RouteCollection routes) {
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute("default", "{controller}/{action}/{id}",
new { action = "index", id = "" },
// Register below the name of all the other controllers
new { controller = @"^(account|support)$" });
routes.MapRoute("home", "{action}",
new { controller = "device", action = "index" });
}
例如。 /富
如果富
是不是一个控制器,那么它当作的行动装置
控制器。
If foo
is not a controller then it's treated as an action of the device
controller.
这篇关于如何配置ASP.NET MVC路由隐藏在&QUOT控制器名称;家&QUOT;页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!