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:
When working with normal MVC routes you may need to manually register certain routes:
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.
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 redirect to the home page, we could use this code:
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.
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.