What Is Route Hijacking In Umbraco

Route Hijacking is an Umbraco concept that you will need to understand if you want to use MVC with Episerver.

What is Route Hi-jacking?

Route Hijacking is a way to hook into the MVC pipeline so you can run custom code and view logic before an Umbraco page is rendered.  Whenever someone visits your site, their request is sent through IIS, into the Global.ascx and into Umbraco.  Umbraco will then get the URL and figure out what document type it will need in order to render the page.  Umbraco will add all this information into the MVC pipeline and the page is sent back through IIS and displayed in the visitor’s browser.

With route hijacking instead of Umbraco sending the data back to IIS directly, the visitor’s request will be redirected to a custom C# controller that you will need to write yourself.  In this custom controller, you can then write any logic to meet your business needs.

The one aspect I’m going to gloss over in this tutorial is the Umbraco part. To make route hijacking work, you need to create a document type in Umbraco. The name of the document type is very important because your controller will need to have the same name as the document type in order for Umbraco to be able to find it.

Setting Up Route Hi-Jacking

As Route Hijacking goes hand-in-hand with MVC, we will need to create three things, a controller, a view model and a view.

Model

Your model will contain properties and logic based on your needs; for this simple example I’m going to have a single property that says ‘Hello World’.  If you want your model to automatically contain certain base Umbraco document properties, you will need to make sure that it inherits from RenderModel. and you pass in an IPublishedContent, via the constructor.

using Umbraco.Web;
using Umbraco.Web.Models;
namespace JonDJones.Models
{
public class CustomModel: RenderModel
{
public CustomModel(IPublishedContent content) : base(content)
{ }
public string HelloWorld() 
{ 
return "Hello World!";
}
}
}

View

In MVC the view is the file where we add HTML.  The controller will pass the Model into the view, meaning any property that you define in your Model will be available for you to use in the view:

@using JonDJones.Models
@inherits UmbracoViewPage<CustomModel>
@{
Layout = "~/Views/Site.cshtml";
}
<div>
@Model.HelloWorld()
</div>

Controller

The last bit of code we need to write is the custom controller.  The controller is the part that hooks everything together.  In my example, I’m making a controller for a document type I created from within the Umbraco backend with an alias of ‘CustomModel’.  Obviously, this is just a simple example to hopefully make this tutorial a little easier to understand.  This is not the name I’d use within a real world solution.  In the real world, I might have document types for Homepage, Blog page, Standard page etc…  The document type alias and the template name are the same but if the template was named BlogPosts then the method would also need to be named BlogPosts.

 

using System.Web.Mvc;
using JonDJones.Models;
using Umbraco.Web.Mvc;
namespace JonDJones.Controllers
{ 
public class CustomModelController : RenderMvcController
{
public ActionResult CustomModel(RenderModel model, int page = 1)
{
var customModel = new CustomModel(model.Content) { Page = page };
return CurrentTemplate(customModel);
}
}
}

 

Just a recap of the code, the name of the controllers and action need to relate to your Umbraco document types name.  The controller will need to inherit from ‘RenderMvcController’.  In your main index, you will then need to create a new instance of the model we created above, pass in the current page object and then return that to the view.   Also remember, the Controllers class name has to end with the world Controller which is an ASP.NET MVC thing.

Tutorial Take Away

The sequence listed above is called route hijacking and it’s the way you tell Umbraco to call your custom MVC controllers whenever a page is loaded.  My example is very plain and simple but gives you, the developer, a lot of power when it comes to architecting your web pages.

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

1 reply

Trackbacks & Pingbacks

  1. […] my route hijacking tutorial when you work with Umbraco and MVC you’ll be creating views, models, and […]

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 *