Route Hijacking is a 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.
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.
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:
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.
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.