And*_*rew 5 asp.net-mvc url-redirection
这可能是其中一个简单的问题.我试图在用户成功通过身份验证后重定向,或将其返回到登录页面.但是Success页面位于不同的路径上,我无法使重定向工作.
以下是我在Globals.asax中的路线:
routes.MapRoute( _
"Default", _
"{controller}/{action}/{id}", _
New With {.controller = "Login", .action = "Index", .id = ""} _
)
routes.MapRoute( _
"Stuff", _
"{controller}/{action}/{id}", _
New With {.controller = "Stuff", .action = "Index", .id = ""} _
)
Run Code Online (Sandbox Code Playgroud)
我有2个控制器:LoginController.vb和StuffController.vb.该Views/Login/Index.aspx文件包含一个带有代码的简单表单:
<form method="post" action="/Login/Authenticate">
Run Code Online (Sandbox Code Playgroud)
将LoginController包含以下代码:
Function Authenticate() As RedirectToRouteResult
' authentication code commented out ;o)
Return RedirectToRoute("Stuff")
End Function
Run Code Online (Sandbox Code Playgroud)
StuffController包含以下内容:
Function Index()
' show stuff..
Return View() ' return /Views/Stuff/Index.aspx
End Function
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所尝试的:
所有这些都会导致浏览器中的重定向循环超时.我错过了什么?!
小智 11
正确的答案是好的,但是:
- 然后,您不仅需要在global.asax中更改值,还需要在使用该技术的所有位置更改值.
我的建议:
return RedirectToRoute("Stuff", (RouteTable.Routes["Stuff"] as Route).Defaults);
Run Code Online (Sandbox Code Playgroud)
现在,在这种情况下,您不会相应地传递控制器/动作的名称,即Stuff/Index.这样您就可以轻松管理更改.
可能是你的Stuff路线与默认路线的形式完全相同,所以当你打电话时
Return RedirectToRoute("Stuff");
Run Code Online (Sandbox Code Playgroud)
生成的url具有以下形式:{controller}/{action}/{id},例如,再次登录/验证,因为您在Login-controller的Authenticate操作中.
尝试
RedirectToAction("Index", "Stuff");
Run Code Online (Sandbox Code Playgroud)
希望有所帮助.