Copied to clipboard

Flag this post as spam?

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


  • Ben Palmer 176 posts 842 karma points c-trib
    Mar 27, 2017 @ 16:04
    Ben Palmer
    1

    Route hijacking with custom controller

    Hi,

    I'm having a little trouble hijacking a route (something that I've done a few times so probably missing something obvious).

    Anyway, here's my controller:

    using System.Web.Mvc;
    using Umbraco.Web;
    using Umbraco.Web.Models;
    using UmbracoSandbox.Models;
    
    namespace UmbracoSandbox.Controllers.Render
    {
        public class UmbracoRssFeedController : Umbraco.Web.Mvc.RenderMvcController
        {
            public ActionResult UmbracoRssFeed(RenderModel model)
            {
                var feedsModel = new UmbracoRssFeedModel
                {
                    Feeds = model.Content.GetPropertyValue("feeds")
                };
    
                return CurrentTemplate(feedsModel);
            }
        }
    }
    

    My model:

    using Umbraco.Web;
    using Umbraco.Web.Models;
    
    namespace UmbracoSandbox.Models
    {
        public class UmbracoRssFeedModel : RenderModel
        {
            public UmbracoRssFeedModel() : base(UmbracoContext.Current.PublishedContentRequest.PublishedContent) { }
    
            public object Feeds { get; set; }
        }
    }
    

    My view:

    @inherits Umbraco.Web.Mvc.UmbracoViewPage<UmbracoSandbox.Models.UmbracoRssFeedModel>
    
    <h1>RSS</h1>
    

    When I load my page, I get the following error:

    Cannot bind source type Umbraco.Web.Models.RenderModel to model type UmbracoSandbox.Models.UmbracoRssFeedModel.
    

    As I said, I feel like I'm missing something obvious but it escapes me right now. I've followed the docs found here with no luck - https://our.umbraco.org/documentation/reference/routing/custom-controllers

    I've also tried the solution in this thread: https://our.umbraco.org/forum/using-umbraco-and-getting-started/75998-update-to-742-all-custom-hijacked-controllers-stopped-working

    If anyone could put me out of my misery, that'd be hugely appreciated.

    Thanks!

  • Marc Goodson 2141 posts 14324 karma points MVP 8x c-trib
    Mar 28, 2017 @ 06:27
    Marc Goodson
    101

    Hi ONeji

    I think you have followed the documentation well!

    The error you have indicates your view is loading... and is strongly typed to UmbracoSandbox.Models.UmbracoRssFeedModel but the model being sent by the controller is of the default type Umbraco.Web.Models.RenderModel.

    So my guess is that the route isn't being hijacked, and the default RenderMvcConroller is firing and sending the default model to your view.

    Route hijacking is by convention, so for your above route to hijack, you would need a document type called:

    UmbracoRssFeed

    published with a corresponding template called:

    UmbracoRssFeed

    So the first thing to check is the naming of your document type and template.

    If you do have a document type called UmbracoRssFeed, then hijacking will look for an ActionResult matching the template name, and if not fallback to use an ActionResult called Index:

    So try adding the following to your controller:

      public override ActionResult Index(RenderModel model)
        {
            //set a breakpoint here
            return CurrentTemplate(model);
        }
    

    Now you can add a breakpoint in visual studio to both of your ActionResults and determine which was/is being executed.

    regards

    Marc

  • Ben Palmer 176 posts 842 karma points c-trib
    Mar 28, 2017 @ 08:20
    Ben Palmer
    0

    Thanks Marc, you've hit the nail on the head!

    I had this working before implementing the model but must have changed the template name without updating the ActionResult name.

    I knew it was something obvious! Thanks for pointing me in the right direction!

  • Marc Goodson 2141 posts 14324 karma points MVP 8x c-trib
    Mar 28, 2017 @ 10:32
    Marc Goodson
    0

    Good to here ONeji!

    I do wonder what type your feeds property is?

    you should be able to cast it to a strong type rather than use 'object'

    model.Content.GetPropertyValue<string>("feeds");
    

    of if it is a complex object you can create you own property value converter

    https://our.umbraco.org/documentation/extending/property-editors/value-converters

    to have

    model.Content.GetPropertyValue<MySuperFeedsClass>("feeds");
    

    (but if it is a core property type there will be a converter in this package you can use: https://our.umbraco.org/projects/developer-tools/umbraco-core-property-value-converters/ now included in Umbraco v7.6+)

    cheers

    Marc

  • Ben Palmer 176 posts 842 karma points c-trib
    Mar 28, 2017 @ 10:36
    Ben Palmer
    0

    Thanks Marc, you can ignore the object type - it was just me being lazy while I set up the route hijacking. I do tend to make use of property converters anyway.

    Appreciate the extra info though :)

    Thanks,

    Ben

Please Sign in or register to post replies

Write your reply to:

Draft