Archive, ASP.NET, Software Design
     

ASP.NET MVC – Decoupling RedirectToAction

The RedirectToAction allows a controller to do an HTTP redirection to an alternative action. This is essentially the MVC equivalent of just doing a Response.Redirect in ASP.NET.

Unfortunately the signature of RedirectToAction looks a little something like:

  RedirectToRouteResult RedirectToAction(string actionName, string controllerName, object values)

This is unfortunate because you are left hard coding your controller and action names as string literals. With modern refactoring tools it would be very easy to break such code. In Scott’s latest video on MVC he discusses using lambdas to decouple link creation, so I figured it was only nature that the MVC framework support a similar concept for redirecting to another action.

As it turns out the framework does not support that, and apparently Matt Hawley experienced the same disappointment. But fortunately he created a wonderful extension method that adds lambda expression support to the RedirectToAction method.

You can read about it, and download the code at http://blog.eworldui.net/post/2008/05/ASPNET-MVC-Expression-Based-RedirectToAction-Method.aspx

Never miss an article! Subscribe to my newsletter and I'll keep you updated with the latest content.

 

About Jason

Jason is an experienced Entrepreneur & Software Developer with a demonstrated history of working in the oil & energy industry. Skilled in Leadership, Mobile Development, Data Synchronization, and SaaS Architecture. Strong engineering professional with a Bachelor of Science (B.S.) in Computer Science from Arkansas State University.
View all posts by Jason →

Leave a Reply

Your email address will not be published. Required fields are marked *