简体   繁体   English

ASP.NET MVC部分视图和重定向

[英]ASP.NET MVC partial views and redirecting

I have a partial view called Login.ascx that has my login boxes that I include on a number of pages throughout my site. 我有一个名为Login.ascx的局部视图,该视图的登录框包含在整个站点的多个页面中。 It works fine when the information is correct but I'm trying to do validation so if the login info is incorrect, I want to redirect the user back to the view they were in before to show them the login errors. 当信息正确但我尝试进行验证时,它可以正常工作,因此,如果登录信息不正确,我想将用户重定向回他们之前的视图,以向他们显示登录错误。 What is the correct way of saying, return to the view you came from? 回到您的观点的正确说法是什么?

If a login fails from any page, I think I would direct them to a login view for the errors instead of the previous page. 如果从任何页面登录失败,我想我会将它们定向到登录视图以查看错误,而不是前一页。 A dedicated login page is likely to have more UI space to display errors, etc. than a login control on another page. 专用登录页面可能比其他页面上的登录控件具有更多的UI空间来显示错误等。 Having said that, you may want to include a returnUrl parameter to the Login action so that when the login is actually successful, the user is directed back to the place they were (or were attempting to get to). 话虽如此,您可能希望在Login操作中包含returnUrl参数,以便在实际成功登录时,将用户定向回到他们曾经(或试图到达)的位置。

Sounds like instead of asking how I do this, you should be asking yourself WHY am I doing it this way. 听起来像是在问自己为什么要这样做,而不是问我怎么做。 Maybe it's a design decision rather than a technical question. 也许这是设计决定,而不是技术问题。

Though if you're really going to have one controller actions for multiple login pages you can try... 虽然如果您真的要对多个登录页面执行一个控制器操作,则可以尝试...

return Redirect(Request.UrlReferrer.ToString());

Or keep the route name in TempData and just use a RedirectToRoute(TempData["LoginRoute"]); 或者将路由名称保留在TempData中,然后使用RedirectToRoute(TempData [“ LoginRoute”]));

Both solutions have a bad code smell though. 两种解决方案都有不好的代码味道。

Note that if you're not checking for cross-site injections that is just going to refer back to the other site. 请注意,如果您不检查跨站点注入,则将参考其他站点。 You may want to do some validation on the referring URL. 您可能需要对引用URL进行一些验证。

For the built-in Login method of the AccountController there is a parameter named returnUrl which you can use like so: 对于AccountController的内置Login方法,有一个名为returnUrl的参数,您可以像这样使用:

  Return Redirect(returnUrl);

or 要么

  Return RedirectToAction(returnUrl);

if you specify the returnUrl parameter as a valid actionlink. 如果您将returnUrl参数指定为有效的操作链接。

I recently had similar problems - you might be able to find something here ... 我最近遇到了类似的问题-您也许可以在这里找到一些东西...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM