为什么 RedirectToRoute(“Default”) 不重定向到根目录?

发布于 2024-09-28 04:00:39 字数 720 浏览 5 评论 0原文

给定这些路线:

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 但我希望它重定向到 /

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

RedirectToRouteResult 结果 = RedirectToRoute("默认");

它有一个值为“Default”的属性 RouteName 和一个没有元素的属性 RouteValues (Count = 0)。我使用 Reflector 进行了检查,发现 null 作为 RouteValueDictionary 在内部传递。

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

为什么它不重定向到 /

Given these routes:

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

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

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

RedirectToRouteResult result = RedirectToRoute("Default");

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.

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

Why doesn't it redirect to /?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感受沵的脚步 2024-10-05 04:00:39

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

要强制其完全回退默认值,只需使用:

RedirectToRoute("default", null");

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

但是我会说首选方法是拥有一个不带任何参数的 Home 路由,这意味着您的重定向不需要大量浮动空值,并且调用 RedirectToRoute("Home") 也很好。和明确的

编辑

这是一个非常古老的答案,正如几个人提到的那样,它似乎并没有为他们工作,因为它的价值我现在不使用这种模式并且明确地。当我需要突破时覆盖控制器和区域,它不仅明显工作得更好,而且当您回到代码时,很高兴明确地看到您正在脱离此上下文并意味着清除某些路由值

。记录 我也更倾向于基于命名路由的 URL 生成而不是基于约定的生成。

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.

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.

Edit

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.

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

违心° 2024-10-05 04:00:39

我不认为 Chao 提到的“null”参数对我有用。也许在以前的 MVC 版本中确实如此,但现在在 MVC 5.2 中,这是我可以让它工作的唯一方法:

RedirectToRoute("Default", new { controller = "", action = "" });

I don't think that "null" parameter Chao mentions has ever worked for me. Perhaps it did in prior MVC versions, but now in MVC 5.2 this is the only way I can get it to work:

RedirectToRoute("Default", new { controller = "", action = "" });
林空鹿饮溪 2024-10-05 04:00:39

如果不查看实际代码来了解发生了什么,要知道原因可能有点棘手,但是这个 论坛帖子确实揭示了一些线索。回答者说,在幕后,它可能会创建一个 RouteValueDictionary ,其中包含您当前所在的控制器和操作,在您的情况下,它将是 Test 的控制器和 Index 的操作。然后,当您调用 RedirectToRoute("Default") 时,匹配默认路由时将使用 RouteValueDictionary 中的控制器和操作,并且您将进入测试控制器中的 Index 操作。

现在,您随时可以执行 Redirect("/") 将您带到网站的主页。

Without looking at the actual code to see what is going on it can be a bit tricky to know why, but this forum post does shed some light. The answerer says that under the covers it might be creating a RouteValueDictionary which would contain the controller and action you are currently in, which in your case would be a controller of Test and an action of Index. Then, when you call RedirectToRoute("Default"), the controller and the action in the RouteValueDictionary will be used when matching the default route and you will be taken to the Index action in your Test controller.

Now you could always do a Redirect("/") to take you to the main page of your site.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文