为控制器设置默认的行动(而不是指数)在ASP.NET MVC 3 [英] Set default action (instead of index) for controller in ASP.NET MVC 3

查看:100
本文介绍了为控制器设置默认的行动(而不是指数)在ASP.NET MVC 3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为控制面板控制器 3动作:摘要详细信息状态,其中没有采取ID或任何其他参数。我想要的网址 /仪表板路由到摘要仪表板的作用控制器,为 /仪表板/摘要呢,但我不能找出正确的方式来添加路由。在的Global.asax.cs ,我有以下几点:

I have a controller called Dashboard with 3 actions: Summary, Details, and Status, none of which take an ID or any other parameters. I want the URL /Dashboard to route to the Summary action of the Dashboard controller, as /Dashboard/Summary does, but I can't figure out the correct way to add the route. In Global.asax.cs, I have the following:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new {controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
    );

routes.MapRoute(
    "/Dashboard",
    "Dashboard",
    new { controller = "Dashboard", action = "Summary" }
    );

有关的第二部分,我也试着:

For the second part, I've also tried:

routes.MapRoute(
    "/Dashboard",
    "{controller}",
    new { controller = "Dashboard", action = "Summary" }
    );

routes.MapRoute(
    "/Dashboard",
    "{controller}",
    new { action = "Summary" }
    );

但在尝试进行访问时,我总是得到404 /仪表板。我是pretty肯定,我失去了一些东西有关的参数格式图路线,但我不知道它是什么...

but I always get a 404 when trying to access /Dashboard. I'm pretty sure I'm missing something about the format for the parameters to MapRoute, but I don't know what it is...

推荐答案

移动你的仪表板路线的缺省路由的前面:

Move your Dashboard route in front of the Default route:

routes.MapRoute(
    "Dashboard",
    "Dashboard/{action}",
    new { controller = "Dashboard", action = "Summary" }
);

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new {controller = "Home", action = "Index", id = UrlParameter.Optional} // Parameter defaults
);

路线的顺序改变了一切。另外,请注意我在控制台路线所做的更改。第一个参数是该路由的名称。二是URL,匹配以控制台启动它的URL,并允许在你的控制台控制器的其他操作。正如你所看到的,将默认为摘要操作。

The order of routes changes everything. Also, notice the changes I made to the Dashboard route. The first parameter is the name of the route. Second is the URL, which match URLs that start with Dashboard, and allows for other actions in your Dashboard controller. As you can see, it will default to the Summary action.

这篇关于为控制器设置默认的行动(而不是指数)在ASP.NET MVC 3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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