Copied to clipboard

Flag this post as spam?

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


  • Simon Dingley 1474 posts 3431 karma points c-trib
    Sep 14, 2018 @ 09:20
    Simon Dingley
    0

    Hijacking Umbraco Routes for AltTemplates

    Is this possible?

    At the moment I can create a controller and have an action execute for a specific template applied to the document but if I have a rewrite rule that does a rewrite to apply an alttemplate to the current page I can't have an action with that name execute in my custom controller.

    Simplified example:

    public class NewsController : RenderMvcController
    {
        // Default action for NewsSection documents
        public ActionResult Index(RenderModel model, int? page)
        {
            var vm = new NewsSection(model.Content);
            var pageNumber = page ?? 1;
            var articles = model.Content.Children<NewsArticle>().Where(n => n.IsVisible());
    
            vm.Articles = articles.OrderByDescending(n => n.ReleaseDate).ToPagedList(pageNumber, 10);
    
            return this.CurrentTemplate(vm);
        }
    
        // Action to execute when the url is called with an alttemplate applied e.g. /news/?alttemplate=tagsearch&tag=tagname
        public ActionResult TagSearch(RenderModel model, int? page, string tag)
        {
            [...]
        }
    }
    

    Interested to know if anyone has got this work or taken a different approach?

    Thanks, Simon

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Sep 14, 2018 @ 11:21
    Dave Woestenborghs
    100

    Hi Simon,

    I think a action with the name with matching the template alias like you have would do the trick.

    Does it hit when you ommit the tagname parameter ?

    And does it hit when you use : /news/tagsearch ?

    Another option is to check if alt templates aren't disabled in config : https://our.umbraco.com/documentation/Reference/Config/umbracoSettings/#webrouting

    Dave

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Sep 14, 2018 @ 12:28
    Simon Dingley
    0

    Hi Dave,

    I think a action with the name with matching the template alias like you have would do the trick.

    As I thought.

    Does it hit when you ommit the tagname parameter ?

    No. I have tried the following urls with no luck hitting my breakpoint within the action named afte the alttemplate:

    • /news/tagsearch
    • /news/?alttemplate=tagsearch
    • /news/?alttemplate=tagsearch&p=1
    • /news/?alttemplate=tagsearch&tag=test
    • /news/?alttemplate=tagsearch&p=1&tag=test

    All of the above actually hit the Index action in my custom controller and not the action I have defined for the alttemplate.

    disableAlternativeTemplates=false in my umbracoSettings and the alttemplate is applied as expected.

    Simon

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Sep 14, 2018 @ 12:31
    Dave Woestenborghs
    0

    Hi Simon,

    Does it if you remove all the params from the action (page and tag)

    Maybe do the same for the Index action.

    Dave

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Sep 14, 2018 @ 12:52
    Simon Dingley
    0

    Does it if you remove all the params from the action (page and tag)

    No, still doesn't.

    And I think you need to override the index method

    If I do that I can't provide my custom parameters but I'm also not sure what the impact of my implementation is (doesn't seem to be any)? That said, I have changed it so that instead of overriding the Index action and sacrificing my custom parameters I am doing it via the template as follows:

    public class NewsController : RenderMvcController
    {
        // Default action for NewsSection documents
        public ActionResult TemplateName(RenderModel model, int? page)
        {
            var vm = new NewsSection(model.Content);
            var pageNumber = page ?? 1;
            var articles = model.Content.Children<NewsArticle>().Where(n => n.IsVisible());
    
            vm.Articles = articles.OrderByDescending(n => n.ReleaseDate).ToPagedList(pageNumber, 10);
    
            return this.CurrentTemplate(vm);
        }
    
        // Action to execute when the url is called with an alttemplate applied e.g. /news/?alttemplate=tagsearch&tag=tagname
        public ActionResult TagSearch(RenderModel model, int? page, string tag)
        {
            [...]
        }
    }
    

    …still, it doesn't advance me any closer to solving my issue, unfortunately.

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Sep 14, 2018 @ 12:54
    Dave Woestenborghs
    0

    Is your TemplateName method hit ?

    Dave

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Sep 14, 2018 @ 12:57
    Simon Dingley
    0

    Dave, immediately after submitting my last response the lightbulb switched on and I realised my mistake after re-reading your initial response. The key being matching the template alias. My template name (which I have been using) is not the same as my template alias! Replacing the name with the alias and things kick into action :)

    Thanks again, always helps to bounce these things off of other people to make it become more obvious what you can't see which is right in front of you!

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Sep 14, 2018 @ 12:59
    Dave Woestenborghs
    0

    Glad to hear you have it working now !!

    Dave

  • Dave Woestenborghs 3504 posts 12134 karma points MVP 9x admin c-trib
    Sep 14, 2018 @ 12:32
Please Sign in or register to post replies

Write your reply to:

Draft