C# 在 ASP.NET MVC 3 中为控制器设置默认操作(而不是索引)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12715667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:12:13  来源:igfitidea点击:

Set default action (instead of index) for controller in ASP.NET MVC 3

c#asp.net-mvcasp.net-mvc-3routesasp.net-mvc-routing

提问by Cody

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

我有一个控制器,它Dashboard有 3 个动作:SummaryDetailsStatus,它们都没有 ID 或任何其他参数。我希望 URL/Dashboard路由到控制器的Summary操作,但我无法找出添加路由的正确方法。在,我有以下内容:Dashboard/Dashboard/SummaryGlobal.asax.cs

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

and

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

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...

但是在尝试访问/Dashboard. 我很确定我遗漏了一些关于 to 参数格式的信息MapRoute,但我不知道它是什么......

采纳答案by Gromer

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

The order of routes changes everything. Also, notice the changes I made to the Dashboardroute. 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 Dashboardcontroller. As you can see, it will default to the Summaryaction.

路线的顺序改变了一切。另外,请注意我对Dashboard路线所做的更改。第一个参数是路由的名称。第二个是 URL,它匹配以 开头的 URL Dashboard,并允许Dashboard控制器中的其他操作。如您所见,它将默认为该Summary操作。

回答by tuxedo25

You need to declare the "Default" catch-all route last.

您需要最后声明“默认”全能路由。

回答by Newred

This set default Action for any Controller asp.net:

这为任何控制器 asp.net 设置默认操作:

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