Copied to clipboard

Flag this post as spam?

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


  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Jun 20, 2013 @ 11:41
    Morten Bock
    0

    Letting the RenderMvcController catch all sub urls

    I would like to be able to do something like this:

    I have some umbraco pages:

    /en/products
    /da/produkter

    These pages are both of "Products", and I have defined a ProductsController : RenderMvcController in my application, that hijacks the route fro those pages.

    Now I woul like to catch these url's with that controller:

    /en/products/{category}
    /en/products/{category}/{sku}
    /da/produkter{category}
    /da/produkter{category}/{sku}

    And it would be really awesome if that was routed to my appropriate actions:

    Index(RenderModel model)
    Index(RenderModel model, string category)
    Index(RenderModel model, string category, string sku)

    How would I achieve this?

    UrlRewriting is probably not the way to go, since I don't know the base url up front, as it may be localized.

    I could maybe set up an INotFound handler, that trims off the end of the url until it matches a node, and the add the rest of the url to the context. Then my Controller would need to delegate to the appropriate actions itself, based on what is in the context.

    Any other options? Any way to make that a feature of the core (or maybe it already is?)?

    EDIT: I'm using v 6.1.1

  • Shannon Deminick 1526 posts 5272 karma points MVP 3x
    Jun 20, 2013 @ 11:52
    Shannon Deminick
    0

    Why not just setup a custom route like normal MVC? Then lookup what you want and return a RenderModel

  • Shannon Deminick 1526 posts 5272 karma points MVP 3x
    Jun 20, 2013 @ 11:57
    Shannon Deminick
    0

    Or, with the new routing logic you'd be able to define your own content lookup and register it on app startup, you'd need to ask stephen about that though.

    It should be pretty easy just to create your own routes though.

  • Stephen 767 posts 2273 karma points c-trib
    Jun 20, 2013 @ 12:26
    Stephen
    0

    Create an IContentFinder, no an INotFoundHandler.

    Ensure that you have a way to identify the "products" and "produkter" nodes eg by having them being of a ProductsPage content type.

    In the IContentFinder, enumerate those nodes and check whether the requested uri starts with the node uri. When that's the case, set the request's published content to the node in question. That does not change the request's uri so the rest of the uri (category, sku) still remains available (no need to put in in a context).

    You may, or may not want to set a template on the node itself. If there's no template then the Index method of the Controller will trigger, and there you can route to your overloaded methods based upon category, sku... and pick the right view.

    Making sense?

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Jun 20, 2013 @ 12:37
    Morten Bock
    0

    Yes, makes sense with the custom IContentFinder.

    Do I just need to implement it, and it will be picked up automagically?

  • Stephen 767 posts 2273 karma points c-trib
    Jun 20, 2013 @ 12:43
    Stephen
    100

    No, there's no magic anymore. You need to explicitely register it. I recommend you browse the slides of my excellent CodeGarden '13 presentation ;-)

    Otherwise:

    public class MyApplication : ApplicationEventHandler
    {
        protected override void ApplicationStarting(…)
        {
            // Insert my finder in first position
            ContentFinderResolver.Current
                .InsertType<MyContentFinder>();
        }
    }

     

    And that one will be picked and run automagically.

    Stephan

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Jun 20, 2013 @ 13:22
    Morten Bock
    0

    Argh, I knew I would miss something by not being at CG :) 

    Thanks again.

  • Jeroen Breuer 4908 posts 12265 karma points MVP 5x admin c-trib
    Jun 20, 2013 @ 13:25
    Jeroen Breuer
    0

    Hi Morten,

    Could you post some example code when you have everything working? Would like to see how it's working with a real world example :-).

    Jeroen

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Jun 20, 2013 @ 13:47
    Morten Bock
    0

    Hi Jeroen

    I will try and get to it, if I end up using this approach. (There are a few factors to count in)

    But I might do a demo of it just for kicks. Then i'll make a repo with it.

Please Sign in or register to post replies

Write your reply to:

Draft