Posts
60
Comments
36
Trackbacks
18
ASP.NET MVC Return User to Previous Link when Registering

So I was playing around with the Login and Sign up links in ASP.NET MVC with the Membership Provider included. I really liked it (and it was easy to understand how to use the Login feature of ASP.NET Memberships). You simply send the user to /Account/Login/?---Return path goes here---. To do this, I used this code here:

/Account/Register/?ReturnUrl=<%= HttpUtility.UrlEncode(Request.FilePath.ToString()) %>#comments

One of the issues with this is that I want the users to be able to sign up as well and be returned to the previous page. This serves many purposes. First and foremost, it’s a blog and the user may not want to navigate back to the section in which they wanted to comment OR perhaps they don’t even know. So I dove in to see if I could produce this behavior which I did with little or no problem.

Plugging in a ReturnUrl so when a user signs up for the first time, they are returned to the page in which they clicked the sign up link.

  • From the page you are sending them to sign up from, put the above link into the href=”” field.
  • Change Login to Register, you should now have something that looks like: (note: the #comments is specific to my application)
    • <a href="/Account/Login/?ReturnUrl= <%= HttpUtility.UrlEncode(Request.FilePath.ToString()) %>#comments">Login </a>
    • Then browse to your account controller and find the method Register.
  • Modify the default line to bring in (string returnUrl) <- Goes inside of the currently empty parenthesis directly following the word Register.
ModifyRegister
  • Insert this before the return View()… if (!String.IsNullOrEmpty(returnUrl)) ViewData["ReturnUrl"] = returnUrl;
  • Just below that, you should see another Register, and you will also want to tack on , string returnUrl to the end of the variables that they are already passing in.
ModifyRegisterPostBack
  • The below picture shows you where you will want to insert the next if statement, and then one more step
RegisterPostBackIfStatement
  • Now navigate to Views/Account/Register.aspx and insert a hidden field (see below) somewhere inside the form tags of that page

In the above picture, the response write that you need is ViewData["ReturnUrl"].

RegisterFormChange

Now test it out and you should have a fully functional (and optional) returnUrl included in your Register module. I appreciate any “Code Reviews” in the way of comments, so if you found that this was useful, please feel free to leave comments here!

 

posted on Saturday, December 06, 2008 11:29 PM Print
Comments
Gravatar
# trackback
Pingback/TrackBack
12/9/2008 12:33 PM
  
trackback
Gravatar
# trackback
Pingback/TrackBack
12/9/2008 12:34 PM
  
trackback
Gravatar
# Trackback
Pingback/TrackBack
12/30/2008 11:55 PM
  
Pingback

Post Comment

Title *
Name *
Email
Url
Comment *  
Please add 4 and 1 and type the answer here:
News
My Developer Notebook! This also happens to be my opinion place. Thanks for coming by.