How to specify return url for link to login form?

It would be simple enough, it would seem, but this is not so - mainly due to the fact that View cannot know which path through the model and controller you got there. Despite this, this is a problem that needs to be resolved:

I have a login link in which the user enters a username and password. When the user clicks "send", I want to redirect to the page that he was viewing. The easiest way to do this seems to indicate the url of the current page as querystring ( ...?returnUrl=... ), and everything else is already built.

But where can I find this url from my view when rendering the link? Naturally, I cannot use RedirectToActionResult, since I do not want to actually translate the user - just display the URL in the link. How?


EDIT:

Now I have begun generosity on this issue, and therefore I consider it necessary to clarify my needs.

I have a UserControl named Login.ascx in my shared folder. In it, I do an ActionLink for the login form and is included in the footer on my main page. I want to do the following:

When an ActionLink is rendered, a route to the view that is currently displayed is added to the querystring returnUrl . If this is done, the user will be returned to the page that he viewed after a successful login, with features that are already built into the ASP.NET MVC Framework.

The reason the previous answers were insufficient is mainly because they did not provide a way to construct the route URL for the current view . I know how to add a query, but I do not know how to find out what to put on this line.

To mark the answer as the answer, I want the method to rebuild the route in the current view shown from the user control on the main page.

+7
asp.net-mvc url-routing
source share
4 answers

The solution is to use HttpContext.Current.Request.RawUrl as follows:

 <%= Html.ActionLink("log on", "LogIn", new { controller = "User", returnUrl = HttpContext.Current.Request.RawUrl }) %> 

Or using the extension method from MVC futures (Microsoft.Web.Mvc.dll):

 <%= Html.ActionLink<AccountController>(c => c.LogOn("name", "password", false, HttpContext.Current.Request.RawUrl), "login here")%> 

ActionController is used by default in mvc, but just adds returnUrl to yours.

+20
source share

One way is to create links that send the user to the login form using returnUrl = / PageToReturnTo ( <a href="/Account/Login/?returnUrl=/Product/10">Login</a> for example). You would like to write this so that the returned url is built from your routes, although manually writing these links on each page would be cumbersome.

The default action for logging into MVC has the returnUrl function already created. You just need to convey the meaning to him, and he will do the rest. Here, copy the signature method pattern from the new project.

 public ActionResult Login(string username, string password, bool rememberMe, string returnUrl) 

Hope that helps ya!

+3
source share

You can use Page.Request.Url to get the route that has currently been rendered.

Although this is a more cosmetic detail, you may want to combine the requests that came through the routes "/" and "/default.aspx" and always return to the route "/". I have a helper property on my main page that does just that.

  protected Uri RouteUrl { get { if (Page.Request.Url.AbsolutePath.StartsWith("/default.aspx", StringComparison.OrdinalIgnoreCase)) { return new Uri(Request.Url, new Uri(Response.ApplyAppPathModifier("~/"))); } return Page.Request.Url; } } 
+1
source share

I do not know about ASPX, but there are several problems that we encountered when creating this:

When the user mistakenly enters their password and cycles through the login page to continue, the goal must be saved.

We also decided to save the POST variables on the page, after which the login "right on time" was required

0
source share

All Articles