我的应用程序使用多个路由。以下是两个相关的:
routes.MapRoute(name: "MyRoute",
url: "Flow",
defaults: new { controller = "MyCtrl1", action = "Index" });
routes.MapRoute(name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "MyCtrl2", action = "Index", id = UrlParameter.Optional });
现在我有一个返回的Action
:
return RedirectToAction("Index", "Flow", new {Id = <currId>});
现在框架可能会进行优化("Default"
路由有一个名为 "Index"
的默认action
),我得到的重定向 url 是:
https://<host>/Flow?Id=<currId>
虽然预期的网址是:
https://<host>/Flow/Index?Id=<currId>
我的问题是我希望"Default"
路由处理请求,而"MyRoute"
处理它。
看起来框架进行了优化,忘记检查优化后预期路由是否更改。
我的问题:
- 你觉得我的路线有问题吗?或者这是一个框架错误?
- 有没有办法阻止这些优化并返回
Flow/Index
?
不使用返回 RedirectToAction() 使用 return RedirectToRoute()。您可以在其中设置自定义路由名称以返回重定向到路由("我的路由")。
参考此链接
http://www.codeproject.com/Articles/641783/Customizing-Routes-in-ASP-NET-MVC
http://www.dotnet-tricks.com/Tutorial/mvc/4XDc110313-return-View%28%29-vs-return-RedirectToAction%28%29-vs-return-Redirect%28%29-vs-return-RedirectToRoute%28%29.html
受保护的内部 RedirectToRouteResult RedirectToAction( 字符串操作名称, 字符串控制器名称)
return RedirectToAction("Index", "MyCtrl2", new {Id = <currId>});
return RedirectToAction("Index", "MyCtrl", new {id= <currId>});
从默认路由中执行默认操作。这将强制始终在创建的 URL 中指定它:
routes.MapRoute(name: "MyRoute",
url: "Flow",
defaults: new { controller = "MyCtrl1", action = "Index" });
routes.MapRoute(name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "MyCtrl2", id = UrlParameter.Optional });