Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Ayo Adesina 430 posts 1023 karma points
    Apr 28, 2017 @ 16:50
    Ayo Adesina
    0

    MVC Route Hijacking - In the Master Page

    Hi Guys,

    As outlined here: https://our.umbraco.org/documentation/reference/routing/custom-controllers

    In my umbraco application most of the pages are rendered by high jacking the umbraco routes using MVC, I have custom view models for each page, and this is working great.

    I want to do the same thing, but I want to provide a custom view model for my master layout page.

    I have tried to do it the same way as the other pages ie. (Controller Name being the same as the document type and the action being the same as the view) Like this:

    public class MasterController : Umbraco.Web.Mvc.RenderMvcController
    {
        public ActionResult Master(RenderModel model)
        {
           var vm = new MasterPageViewModel();
    
            //Do something
    
           return View(vm);
        }
    }
    

    But I can't get VS to hit this method.

    So is what I am trying to do possible? Do I need to do something different because its the master page?

  • Marc Goodson 2126 posts 14217 karma points MVP 8x c-trib
    Apr 29, 2017 @ 08:39
    Marc Goodson
    2

    Hi Ayo

    I think I understand what you mean

    Your Master view isn't every called by itself and isn't linked to a document type called Master?, and so there isn't a route here to hijack!

    If I understand correctly you want to use some nice strongly typed model properties in your master view layout that your other templates inherit from but all your other routes have different models specific to their document types - eg if you strongly typed your master view to use a ProductModel, it will be fine for the Product page, but then error when your AboutUs page is hijacked and the AboutUsModel used...

    One way to look at this is to have a BaseViewModel (or interface it depends on which props you need to use) containing all the properties you might want to use in your master view. Your page specific models would then inherit from this base class, and your hijacked routes would populate these base properties along with the properties for the specific document type, and then you can use the strongly typed properties of the BaseViewModel in your master template...

    So your master view would inherit

    @inherits UmbracoTemplatePage<BaseViewModel>
    

    and your BaseViewModel would have your common properties

    public class BaseViewModel : RenderModel {
    
    public string SiteName {get;set;}
    public IEnumerable<IPublishedContent> SiteNavigation{get;set;}
    }
    

    .. if I've understood correctly :-)

  • Ayo Adesina 430 posts 1023 karma points
    May 02, 2017 @ 09:58
    Ayo Adesina
    1

    Thanks Mark, I have taken your approach and introduced a base view model and its working exactly how I wanted it to, so thank you!

    One thing that was wrong in your answer though was how the Master Page Inherits the Model. (guessing that's just an oversight)

    it should be:

    @inherits UmbracoViewPage<BaseViewModel>
    

    instead of

    @inherits UmbracoTemplatePage<BaseViewModel>
    

    Just in case someone else needs this.

    Thanks again Marc

  • Marc Goodson 2126 posts 14217 karma points MVP 8x c-trib
    May 02, 2017 @ 19:50
    Marc Goodson
    0

    Great, glad it helped put you on the right track!

Please Sign in or register to post replies

Write your reply to:

Draft