Strongly Typed RedirectToAction

A strongly typed version of the RedirectToAction method is available that uses lambda expressions. This method can either be used to redirect to an action on the same controller or a different controller.

The strongly typed RedirectToAction is available as an extension method, meaning it is not necessary to inherit from a specific base class, but you do have to have the MvcContrib namespace imported.

public class MyController: Controller
{
	public ActionResult Index() 
	{
		return View();
	}

	public ActionResult RedirectActionOnSameController()
	{
		return this.RedirectToAction(c => c.Index());
	}

	public ActionResult RedirectActionOnAnotherController()
	{
		return this.RedirectToAction<AnotherController>(c => c.List());
	}
}


public class AnotherController: Controller
{
	public ActionResult List() 
	{
		return View();
	}
}

Last edited Dec 4, 2009 at 4:23 PM by JeremyS, version 2

Comments

JonathanConway May 24, 2011 at 3:30 PM 
I'm trying to use the strongly-typed 'Action<T>' method (UrlHelper), but I'm constantly getting this problem:
If I include at least one Area in my MVC project, it *always* uses that area, instead of using the correct area in which my controller is located. On inspecting the source, it seems to derive this behavior from the 'LinkBuilder' class in Microsoft.Web.Mvc.

However, when I downloaded the source to this (aspnet-66799.zip) I couldn't get any of it to compile. It kept throwing compile error after compile error about unreferenced DLLs, type conversion errors, etc. I'm pretty shocked - I mean, how can Microsoft people work with code like this?

JeremyS Feb 25, 2010 at 7:35 AM 
@ozz, please use the mailing list to discuss issues - the wiki comments are not monitored.

ozz Feb 25, 2010 at 12:13 AM 
Issue with RedirectToAction<Controller>(action)

The following code will throw an exception in the Binder when it tries to convert the string 'MyCode.Account' to the type of parameter 'account' (i.e. MyCode.Account) of Action2. Is the underlying code for RedirecToAction<T> calling the MyCode.Account.ToString() method (which is not overridden and defaults to object.ToString() which outputs the typename)?????

namespace MyCode
{
class Account
{
public string Foo { get; set; }
public string Bar { get; set; }
}

class MyController
{
public ActionResult Action1(Account account)
{
RedirectToAction<MyController>(a => Action2(account)); /// <== blows up doing this redirect
}

public ActionResult Action2(Account account)
{
return null;
}
}
}

JeremyS Dec 4, 2009 at 4:23 PM 
@adamtolley,

You are correct. I've now fixed it.

adamtolley Dec 4, 2009 at 3:44 PM 
In the example line:
return this.RedirectToAction<CustomerController>(c => c.List());
should <CustomController> be replaced by <AnotherContoller>?

If not, what is the purpose of showing the AnotherController Class?

krugs525 Nov 5, 2009 at 4:42 PM 
More details on how to pass parameters when doing strongly typed redirects:
http://jonkruger.com/blog/2009/04/06/aspnet-mvc-pass-parameters-when-redirecting-from-one-action-to-another/