asp.net-mvc ASP.Net MVC - 重定向到路由提供重定向循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/237977/
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
ASP.Net MVC - redirecting to route gives Redirect Loop
提问by Andrew
This is probably one of those easy questions.. I'm trying to redirect the user after they've successfully authenticated, or return them back to the login page. But the Success page is on a different route and I can't get the redirection to work..
这可能是那些简单的问题之一。我试图在用户成功通过身份验证后重定向用户,或者将他们返回到登录页面。但是成功页面在不同的路线上,我无法使重定向工作..
Here are my routes in Globals.asax:
这是我在 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 = ""} _
)
I've got 2 Controllers: LoginController.vband StuffController.vb. The Views/Login/Index.aspxfile contains a simple form with the code:
我有 2 个控制器:LoginController.vb和StuffController.vb. 该Views/Login/Index.aspx文件包含一个带有代码的简单表单:
<form method="post" action="/Login/Authenticate">
The LoginControllercontains the following code:
将LoginController包含以下代码:
Function Authenticate() As RedirectToRouteResult
' authentication code commented out ;o)
Return RedirectToRoute("Stuff")
End Function
And the StuffController contains the following:
StuffController 包含以下内容:
Function Index()
' show stuff..
Return View() ' return /Views/Stuff/Index.aspx
End Function
Here's what I've tried so far:
这是我迄今为止尝试过的:
- Function Authenticate()
- Function Authenticate() As ActionResult()
- Function Authenticate() As RedirectToRouteResult()
- 函数验证()
- 函数 Authenticate() 作为 ActionResult()
- 函数 Authenticate() 作为 RedirectToRouteResult()
all of which cause a Redirect Loop timeout in the browser. What am I missing?!
所有这些都会导致浏览器中的重定向循环超时。我缺什么?!
回答by Tengiz
Correct answer is good, but:
正确答案很好,但是:
- what if you ever want to change the controller/action name from Staff/Index to something else?
- 如果您想将控制器/操作名称从 Staff/Index 更改为其他名称怎么办?
-then you will need to change the values not only in global.asax, but also in all the places where you used the technique.
- 那么您不仅需要更改 global.asax 中的值,还需要更改使用该技术的所有位置的值。
My suggestion:
我的建议:
return RedirectToRoute("Stuff", (RouteTable.Routes["Stuff"] as Route).Defaults);
Now, in this case, you don't pass the names of controller/action which is Stuff/Index accordingly. This will let you manage changes easily.
现在,在这种情况下,您不会相应地传递控制器/操作的名称,即 Stuff/Index。这将使您轻松管理更改。
回答by liggett78
Could it be that your Stuff route has exactly the same form as the default one, so when you call
可能是您的 Stuff 路由与默认路由具有完全相同的形式,因此当您调用
Return RedirectToRoute("Stuff");
the resulting url has the form: {controller}/{action}/{id}, e.g. Login/Authenticate again, since you are inside Login-controller's Authenticate action.
生成的 url 具有以下形式:{controller}/{action}/{id},例如再次登录/验证,因为您在登录控制器的验证操作中。
Try to
尝试
RedirectToAction("Index", "Stuff");
Hope that helps.
希望有帮助。
回答by tvanfosson
I fail to see where you are setting the authentication cookie or marking the user as having been authenticated in any way. Is that in the code that you have omitted?
我看不到您在哪里设置身份验证 cookie 或以任何方式将用户标记为已通过身份验证。是在您省略的代码中吗?
回答by tvanfosson
try
尝试
routes.MapRoute( _
"Stuff", _
"",_
New With {.controller = "Stuff", .action = "Index", .id = ""} _
)

