Skip to the content

Route Hijacking in Umbraco 8

Just a Quick break down Route Hijacking is when you take control over how your Umbraco 8 MVC framework runs, you intercept after the Umbraco 8 Out of the box controller does its thing and you add another controller what inherits from your Umbraco controller and then adds extra features what you need to make your site run how you need it to.

So firstly I create a new Doc Type and View in my Umbraco 8 Back-office and i called them Content you can name your how you like

Next you want to create a new controller in you Visual Studio Solution and make sure all your Doctype, View and Controller all have the same name space but your controller needs to have controller in the name (eg. "Content.chhtml" "ContentController.cs")

Now I created a new class called "ContentViewModel.cs" 

Here i will show you my code so you can see what code i have added.

This is my View template:

@inherits UmbracoViewPage<Playground.Core.Models.ContentViewModel>
@{
Layout = "master.cshtml";
} <h1>@Model.PageContent.HeaderText</h1>
<h2>@Model.PageContent.MainText</h2>
<h3>@Model.TestString</h3>
}

This is my Controller:

using Playground.Core.Models;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;
using System.Web.Mvc;
using System.Collections.Generic;
using System; namespace Playground.Core.Controllers
{
public class ContentController : RenderMvcController
{
public override ActionResult Index(ContentModel model)
{
var vm = new ContentViewModel(model.Content) { };
vm.TestString = "Hello";
return View("/Views/content.cshtml", vm);
}
}
}

And here is my ViewModel Class:

using Umbraco.Core.Models.PublishedContent;
using Playground.Core.Models;
using Umbraco.Web.Models;
using System.Collections.Generic; namespace Playground.Core.Models
{
public class ContentViewModel : ContentModel
{
public ContentViewModel(IPublishedContent content) : base(content) { PageContent = (Content)content;
} public Content PageContent { get; set; } public string TestString { get; set; } public List<Banner> Banners { get; set; }
}
}
}

Now if i need to add any extra functionality you can add it into your controller and it will be implemented just asif you added it as razor script on your view but now all your code is a lot neater and easier to read.

About the author

Nathan Reece

.Net MVC Developer | Umbraco Novice

Hey Guys,

I'm an New Developer with a passion for coding. I absolutely love working with Umbraco CMS and appreciate the Umbraco community even more.

I've got 3 years+ .Net experience and 5 years+ experience working in the Web Develepment industry, both coding and marketing job websites. I wanted to use my skills to help give something back to this awesome community and UmbraJobs.com seems like the right place to do just that.

Follow me on social media

If you're an Umbraco professional, partner, employer, recruiter or a passionate Umbraco community builder then I'm more than happy to hear from you. Follow me on my social media channels and please reach out if you have any needs, want help or consultation with anything Umbraco related or just want a general chat.

comments powered by Disqus

Blog Filter


How we can help?

Need help with an Umbraco project?

Need help with a project but not too sure who to approach? Send us your project brief and budget. We will provide a free consultation and can help you gather quotes from the best and most suitable freelancers or agencies.

Looking to hire an Umbraco Professional?

Have you got job vacancy and want to hire an Umbraco professional? Post a job on our website for free. Alternatively let us know your requirements and we will send suitable prospects your way.

Claim your free profile!

Are you an Umbraco Freelance Developer or Umbraco partner that wants to advertise on our site? If you work with Umbraco or registered as an Umbraco partner then you can create a profile for free.

Let's build the Umbraco Community

We're big on building the Umbraco community and we think you guys are awesome! If there's anyway at all we can help then please reach out.