Return URL's in ASP.NET MVC Postbacks
One of the neat things about the ASP.NET MVC framework is the ability to reuse bits of user interface code. A problem can present itself when you create a reusable view that posts data. Hard coding a redirect can lead to a difficult and frustrating user experience. I'm going to show you a simple way to redirect back to the originating page.
The theory is simple, pass the return URL into your form and store it as a hidden field in the form. When the form posts, you can use this field to redirect appropriately.
As the most basic example, you can call a Create action in your controller from any page on your site and it will redirect back to the originating page. The controller code to render your form may look like something like:
public ActionResult Create()
{
// pass the referrer to the view
ViewData["referringUrl"] = Request.UrlReferrer;
// do some other stuff
return View();
}
Next you will want to create a hidden field in your view so that the referring url will be posted.
<%= Html.Hidden("referringurl") %>
// have your form here
<% Html.EndForm() %>
Now in your controller, you use the referral url to redirect appropriately:
public ActionResult Insert(string referringUrl)
{
// perform your operations
if(string.IsNullOrEmpty(referringUrl))
// redirect to a default page
else
return Redirect(referringUrl);
}
As a final caution, IE has issue with tracking the referring url when JavaScript is used for redirection. Refer to the blog post Where, oh where is my referer (sic) for more information.
If you want to manually provide the ReturnUrl to your form, you can simply pass it into the Get Create Action Method and use the Request.UrlReferrer as a backup:
public ActionResult Create(string urlreferrer)
{
// pass the referrer to the view
if(!string.IsNullOrEmpty(urlreferrer))
ViewData["referringUrl"] = urlreferrer;
else
ViewData["referringUrl"] = Request.UrlReferrer;
// do some other stuff
return View();
}
Finally, if you are working with a partial, simply pass the ViewData["referringUrl"] to the partial view and it should perform the same as above.
Recent blog posts
- Quick Memory Improvement for Firefox
- Canned VirtualPC Instances for IE 6, 7, 8 on XP/Vista
- Checking assembly dependencies for .NET
- Google's Public DNS
- Server Utility Functions for Non-Web Apps
- reCAPTCHA for ASP.NET MVC that uses ModelState
- Adding a container to ValidationSummary helper in ASP.NET MVC
- Generic XML Serialization Class
- Re-throwing Exceptions in C# with InternalPreserveStackTrace
- Solving xsd generation error: 'The element .... is missing'
Recent comments
- Thank you
7 weeks 4 days ago - so very helpful kenny. thank
24 weeks 1 day ago - Sorry, apparently the drupal
27 weeks 2 days ago - A better method
30 weeks 3 days ago - No Source Code?
36 weeks 1 day ago - security
46 weeks 14 hours ago - Nice simple solution
1 year 30 weeks ago - That's quite an interesting
1 year 47 weeks ago - Small Complaint
1 year 47 weeks ago - NIceeee
2 years 2 weeks ago
Comments
#1 Anonymous
Thank you!