为什么RedirectToRoute("Default")不能重定向到根目录? [英] Why does RedirectToRoute("Default") not redirect to the root?

查看:185
本文介绍了为什么RedirectToRoute("Default")不能重定向到根目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下路线:

routes.MapRoute("Test", "test", new { controller = "Test", action = "Index" });
routes.MapRoute("Default", "{controller}/{action}/{id}", 
  new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

如果我从TestController的Index操作调用RedirectToRoute("Default"),它将重定向到/test,但是我希望它重定向到/

If I call RedirectToRoute("Default") from the Index action of the TestController it redirects to /test but I expected it to redirect to /

在调试会话期间将其返回之前,我检查了调用RedirectToRoute("Default")的结果.

I checked the result of calling RedirectToRoute("Default") before returning it during a debugging session.

RedirectToRouteResult结果= RedirectToRoute(默认");

RedirectToRouteResult result = RedirectToRoute("Default");

它具有值为"Default"的属性RouteName和不包含元素(Count = 0)的RouteValues属性.我使用Reflector检查过,并将null作为RouteValueDictionary在内部传递.

It has a property RouteName with a value "Default" and a property RouteValues with no elements (Count = 0). I checked using Reflector, and null is passed internally as the RouteValueDictionary.

同样,我希望给定应用程序中定义的路由的默认值,它将重定向到HomeController的Index视图.

Again, I would expect that given the defaults for the route defined in my application, it would redirect to Index view on the HomeController.

为什么不重定向到/?

推荐答案

用于填充当前操作的RouteValueDictionary用于填充Controller,Action,ID.这通常是您想要的功能,它使您可以执行<%:Html.ActionLink("MyAction")%>之类的操作而无需在视图中指定控制器.

The RouteValueDictionary filled in to the current action is being used to fill in the Controller, Action, ID. This is usually the functionality you want and lets you do things like <%:Html.ActionLink("MyAction")%> without needing to specify your controller in a view.

要将其强制设为完整的后备默认设置,只需使用:

To force it to the complete fall back default just use:

RedirectToRoute("default", null");

第二个参数用于您的路由值,并通过指定它来覆盖预先存在的值.

The second argument is for your routevalues and by specifying it you override the preexisting values.

但是,我想说的首选方法是拥有一个不带任何参数的本地路由.这意味着您的重定向将不需要大量的空值,并且对RedirectToRoute("Home")的调用也很清晰.

However I would say the preferred way would be to have a Home route which will not take any parameters. This means your redirects will not need a load of nulls floating around and a call to RedirectToRoute("Home") is also nice and explicit.

修改

这是一个非常古老的答案,正如一些人提到的那样,似乎并没有为他们工作.值得的是,现在我不需要使用此模式,而是在需要突破时显式覆盖控制器和区域.显然,这样做不仅效果更好,而且当您回到代码中时,很高兴能清楚地看到您脱离了这种情况,并且意味要消除某些路由值.

This is a really old answer and as a couple of people have mentioned doesn't seem to have been working for them. For what it's worth I now don't use this pattern and explicitly override controller and area when I need to break out. Not only does it apparently work better but when you come back to the code it's good to see explicitly that you're coming out of this context and mean to blank out certain routevalues.

为了便于记录,我也更倾向于基于命名路由的URL生成,而不是基于约定的生成.

For the record I have also tended more towards named route based URL generation over convention based generation.

这篇关于为什么RedirectToRoute("Default")不能重定向到根目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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