How To Redirect To Another Episerver Page From A MVC Controller

When we work with Episerver, it is sometimes necessary to have to do things like re-direct to different actions or pages from an MVC controller. In today’s guide, I’m going to cover a few simple ways to achieve this:

In normal MVC we can re-direct to a controller using a RedirectToAction, like so:

public ActionResult Index()
{
return RedirectToAction("Index", "Home");
}

When working with normal MVC routes you may need to manually register certain routes:

public class EPiServerApplication : EPiServer.Global
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
}
protected override void RegisterRoutes(RouteCollection routes)
{
base.RegisterRoutes(routes);
routes.MapRoute(
"Default",                                             
"{controller}/{action}/{id}",                           
new { controller = "Home", action = "Index", id = "" });
}
}

Now this can work in some instances, however, Episerver routing works a bit differently. In a normal MVC site, the Url will match directly to a controller and action in the MVC website. In Episerver, we work with virtual pages. Virtual pages DO NOT directly match a controller and action. Instead, Episerver actually disables the routing to directly call a pages controller with an Id. If you had a content page type for example with an Id of 23, you cannot directly call a controller by using this type of Url www.website.com/contentpagecontroller/id/23 (unless you manually allow these types of routes to work). Instead, you need to re-direct slightly differently

Redirect using a Content Reference

Just because we use Episerver it doesn’t mean we still can’t use RedirectToAction. If we want to use RedirectToAction we need to supply the node segment with a valid content reference, so say we want to re-direct to the home page, we could use this code:

public ActionResult Index()
{
return RedirectToAction("Index", new { node = ContentReference.StartPage });
}

Redirect using UrlResolver

The second option to re-directing is via friendly Url and doing an explicit redirect. We can get an Url based on a content reference from Episerver using the UrlResolver API.

public ActionResult Index()
{
var homepage =
ServiceLocator.Current.GetInstance<IContentRepository>().Get<IContent>(ContentReference.StartPage);
var homepageUrl = ServiceLocator.Current.GetInstance<UrlResolver>()
.GetVirtualPath(homepage.ContentLink, homepage.LanguageBranch);
return Redirect(homepageUrl);
}

This method is a little more verbose but it will let you redirect to the default action for an Episerver page type. Simply, use UrlResolver, GetVirtualPath method by passing in a content reference and Episerver will return the URL you need to call to do an explicit redirect.

Redirect To Current Pages Index Method

public class RedirectToIndex: ActionResult
{
public override void ExecuteResult(ControllerContext context)
{
((Controller) context.Controller).ActionInvoker.InvokeAction(context, "Index");
}
}

Jon D Jones

Software Architect, Programmer and Technologist Jon Jones is founder and CEO of London-based tech firm Digital Prompt. He has been working in the field for nearly a decade, specializing in new technologies and technical solution research in the web business. A passionate blogger by heart , speaker & consultant from England.. always on the hunt for the next challenge

More Posts

2 replies
  1. Vincent
    Vincent says:

    Thanks Jon.

    Really useful one.

    Sometimes, I have another action in the controller, that serves up ajax call. Without hard code the url or change to web Api, is there a better solution to generate the url?

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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