如何配置 ASP.NET MVC 路由以隐藏“home"上的控制器名称?页? [英] How do I configure ASP.NET MVC routing to hide the controller name on a "home" page?

查看:32
本文介绍了如何配置 ASP.NET MVC 路由以隐藏“home"上的控制器名称?页?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从这个问题开始:

<块引用>

带有默认控制器的 ASP.NET MVC 路由

我有一个类似的要求,我的最终用户不希望在他们的应用程序的登陆或主页"的 URL 中看到控制器名称.

我有一个名为 DeviceController 的控制器,我希望它成为主页"控制器.这个控制器有很多动作,我想使用如下 URL:

<上一页>http://example.com -> 调用 Index()http://example.com/showdevice/1234 -> 调用 ShowDevice(int id)http://example.com/showhistory/1224 -> 调用 ShowHistory(int id)

我还需要为此控制器生成链接,以省略 url 的 /device 部分.

我还有一些其他的控制器,例如 BuildController:

<上一页>http://example.com/buildhttp://example.com/build/status/1234http://example.com/build/restart/1234

等等.这些控制器的 URL 没有问题.

问题是,即使在研究了上面提到的问题的答案之后,我似乎也无法理解路由.

谁能提供一个代码示例来解释如何做到这一点?

我正在使用 ASP.NET MVC2.

解决方案

试试这个:

 private void RegisterRoutes(RouteCollection routes) {routes.IgnoreRoute("{resource}.axd/{*pathInfo}");routes.MapRoute("default", "{controller}/{action}/{id}",新的{动作=索引",ID="},//在所有其他控制器的名称下面注册新的 { 控制器 = @"^(account|support)$" });routes.MapRoute("home", "{action}",新的{控制器=设备",动作=索引"});}

例如/foo

如果 foo 不是控制器,则将其视为 device 控制器的操作.

Following on from this question:

ASP.NET MVC Routing with Default Controller

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.

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)  

I also need links generated for this controller to leave out the /device part of the url.

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  

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.

Can someone provide a code sample explaining how to do this?

I'm using ASP.NET MVC2.

解决方案

Try this:

   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" });
   }

e.g. /foo

If foo is not a controller then it's treated as an action of the device controller.

这篇关于如何配置 ASP.NET MVC 路由以隐藏“home"上的控制器名称?页?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆