Copied to clipboard

Flag this post as spam?

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


  • Anton 36 posts 67 karma points
    Apr 12, 2013 @ 08:33
    Anton
    0

    How to debug umbraco MVC conventions?

    Hi,

    In v6 i created a doctype called Master, which contains things such as Title, Meta Description and Meta Title.  I have a corresponding controller named MasterController:

    public class MasterController : RenderMvcController
    {
        public override ActionResult Index(Umbraco.Web.Models.RenderModel model)
        {
            var page = GetPageModel<Page>(model);
            return CurrentTemplate(page);
        }
        public ActionResult Master(Umbraco.Web.Models.RenderModel model)
        {
            var page = GetPageModel<Page>(model);
            return CurrentTemplate(page);
        }
    
        public T GetPageModel<T>(RenderModel model) where T : Page, new()
        {
            var t = new T
            {
                BrowserTitle = model.Content.GetPropertyValue<string>("browserTitle"),
                Title = model.Content.GetPropertyValue<string>("title"),
                BodyText = model.Content.GetPropertyValue<string>("bodyText")
            };
    
            return t;
        }
    }

    A snippet of my master view is:

     

    @model Models.Page
    @{
        Layout = null;
    }
    
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta charset="utf-8">
        <title>@Model.BrowserTitle</title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="@Model.MetaDescription">
        <meta name="keywords" content="@Model.MetaTitle">
        <meta name="author" content="">
    </head>

    For some reason i canot intercept the route and my custom code in the controller never fires.  Im following the documentation and my understanding of the Umbraco conventions the Index action in the MasterController should fire but it does not.  So i created another Action method called Master, which also does not fire.

    Based on my code example which controller is the request routed too?  Any easy ways to debug this?  

    Also is there anyways we can override the Umbraco convention, which IMO is a bit of a black box.

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Apr 12, 2013 @ 16:42
    Andy Butland
    0

    Can't be very helpful here... other than to say that looks right to me, and I've managed to get something set up the same way as you describe working.  The convention is that if a controller matching a document type is found, it'll route to the Index action - unless there's also an action that matches the template name, and then it'll use that.  So in your case the Index method should be routed too.

    I'll post my code that works... but it's the same as you have as far as I can tell:

        using System.Linq;
        using System.Web.Mvc;
        using Umbraco.Web.Models;
        using Umbraco.Web.Mvc;
    
        public class VenuesListingController : RenderMvcController
        {
            public override ActionResult Index(RenderModel model)
            {
                // Redirect to first node (if exists)
                if (model.Content.Children.Any())
                {
                    return Redirect(model.Content.Children.First().Url);
                }
    
                return Redirect("/");
            }
        }
  • Charles Afford 1163 posts 1709 karma points
    Apr 12, 2013 @ 22:15
    Charles Afford
    0

    Hi i could be wrong here, but i think you need a controller name of Master to match your doctype?  Charlie :).

  • Anton 36 posts 67 karma points
    Apr 13, 2013 @ 00:28
    Anton
    0

    Hi Charles,

    My DocType is named "Master" and the controller is named "MasterController" - This is what the umbraco convention states.

    Thanks 

  • Charles Afford 1163 posts 1709 karma points
    Apr 13, 2013 @ 15:18
    Charles Afford
    0

     

    That example does feel right.  How is it going to know what your doctype name is?

    If you are trying to hijack the route.  Then your controller need to be the same as your doctype.   So:

    • Document Type name = controller name
    • Template name = action name, but if no action matches or is not specified then the 'Index' action will be executed.
    In this case you would need a controller of Master
    and then an action of Index which will be the default.
    Hope this helps.  I could be wrong!.  The first example here does not feel right but could be wrong :).  http://our.umbraco.org/Documentation/Reference/Mvc/custom-controllers.

     

  • Charles Afford 1163 posts 1709 karma points
    Apr 13, 2013 @ 15:25
    Charles Afford
    0

    Are you able to debug, are you hitting any breakpoints?

  • Anton 36 posts 67 karma points
    Apr 13, 2013 @ 15:30
    Anton
    0

    Nope breakpoints not hitting.either in overidden Index action or the Master action.  Not sure i understand.  If my doctype is named "Master" then what should my controller name be?  Please see the code example in my OP

  • Charles Afford 1163 posts 1709 karma points
    Apr 13, 2013 @ 15:38
    Charles Afford
    0

    I would try calling your doctype Master and see if you can hit the index action.  If then have you got any allowed templates of the doc type?  If you have then try:

     

    public class MasterController:RenderMvcController
    {
           
    public override ActionResult yourallowedtemplatename(Umbraco.Web.Models.RenderModel model)
           
    {
                   
    var page = GetPageModel<Page>(model);
                   
    return CurrentTemplate(page);
           
    }

    and see if that break point hits :).  What do you mean by your OP?  :).

  • Anton 36 posts 67 karma points
    Apr 13, 2013 @ 15:40
    Anton
    0

    My original post.  The first post in this thread already contains this code.

  • Charles Afford 1163 posts 1709 karma points
    Apr 13, 2013 @ 15:45
    Charles Afford
    0

    Ah i see yea i looked at that :)  Did my example above work?

Please Sign in or register to post replies

Write your reply to:

Draft