In this tutorial, you will learn how to redirect a page request in code within an Episerver CMS powered website. Within a CMS like Episerver, underlining routing patterns are compared to vanilla MVC. This means that if you tried to trigger a normal redirect to another controller using a vanilla MVC pattern, you will encounter a 404 error. When redirecting, to ensure that a particular page controller triggers instead of throwing a 404 error, you may need to write Episerver specific redirection code. If you want to know what Episerver specific code looks like, read on 🔥🔥🔥
In normal MVC we can redirect to a controller using RedirectToAction
. The code to enable this is shown below:
Depending on what you are trying to get working when dealing with redirect and routing, you may need to add custom rules into the routing table. A routing rule could look like this:
As mentioned, Episerver routing works a bit differently. In a normal MVC site, the segments within a Url
will normally map to a controller and an action. In Episerver, we work with virtual pages. Virtual pages DO NOT directly match a controller and action. Episerver actually disables the default ASP.NET MVC routing rule and defines its own one. The default Episerver page routing rule is based on the page-type controller and the page Id
. If you had a page of type ContentPage
, with an Id
of 23
, you cannot directly call a controller by using the normal MVC routing rule. If you tried typing this Url
into an Episerver CMS-powered website, www.website.com/contentpagecontroller/id/23
a 404 error would be thrown.
RedirectToAction with Node Segment: It is for this reason, you need to write slightly different redirect code within an Episerver CMS powered website. You can still use RedirectToAction
with Episerver, the difference is you need to supply the node
segment. In order to redirect to a page controller successfully, you need to add node
AND a valid content Id
. Say you want to redirect to the home page, you could use this code:
UrlResolver: There is also an alternative way to perform a redirect. You can generate the Url
you want to redirect to and explicitly perform a redirect. By using the UrlResolver
API, you can generate a URL, based on a content reference. With a valid Url
, you can then redirect the request. The code to do this is shown below:
This approach results in more verbose code, however, it will work. On-Line 6, the UrlResolver.GetVirtualPath()
method is used to generate a Url
based on a ContentReference
. On-Line 9 an explicit redirect is made. Simples 💥
ActionInvoker.InvokeAction: You can also do a redirect to the same pages Index
action using ActionInvoker.InvokeAction()
. The code is shown below:
This code will take the current page request and trigger a self redirect. This can be handy when dealing with validation and authentication requirements. In general, I use the other techniques over this though 🤔
You now know three techniques that will allow you to successfully perform a redirect within an Episerver CMS-powered controller. Happy Coding 🤘