Copied to clipboard

Flag this post as spam?

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


  • Kyle 3 posts 50 karma points
    Aug 15, 2014 @ 18:20
    Kyle
    0

    How do I intercept all routes that have a base controller parent path in them in Umbraco 7

    I have looked all around at Umbraco route hijacking and using IContentFinder and IUrlProvider and I am still a little lost on how to do what I want.

    I have a controller that matches a document type which I have a base page for in Umbraco.

    I made a document type called "Ads" and a controller called "AdsController: RenderMvcController"

    I have a basic method to catch all like this

    public ActionResult Index(RenderModel model)

    It works if the route is like this

    http://www.example.com/Ads?parent=Cars&child=American

    but I want it to hit if I do this :

    http://www.example.com/Ads/Cars/American

    So basically nice MVC routing. I think content provider only lets you find content that is in your actual tree and this content isn't. I am going to find it using C# in the DB and render a custom view and return it using a custom view model.

    I know how to do the view part. Just need to know how to hijack all routes under a parent route so my base controller method hits without having to create a bunch of templates or action methods for each AD category type.

  • Andy Butland 422 posts 2334 karma points MVP 4x hq c-trib
    Aug 19, 2014 @ 11:04
    Andy Butland
    0

    Hi Kyle

    I did something similar to this using a content finder - adapting for your case I think it'll be something like this:

            public bool TryFindContent(PublishedContentRequest request)
            {
                // We are looking for URLs of the form (/[ads landing page]/[parent category]/[child category]/
                var urlParts = request.Uri.GetAbsolutePathDecoded().Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                if (urlParts.Length == 3)
                {
                    // Check to see if request is below the ad landing page, if so do silent redirect to it passing the categories
                    var landingPageUrl = "/" + string.Join("/", urlParts[0]);
                    var firstLevelContent = request.RoutingContext.UmbracoContext.ContentCache.GetByRoute(landingPageUrl);
                    if (IsAdPage(firstLevelContent))
                    {
                        // Store the store Id in the HTTP context
                        HttpContext.Current.Items["parent"] = urlParts[1];
    HttpContext.Current.Items["child"] = urlParts[2]; // Set the content to be the ads landing page - which will take us to our hijacked // route controller to render the detail page request.PublishedContent = firstLevelContent; // Return true to indicate content found return true; } } return false; }

    private bool IsAdPage(IPublishedContent content)
    { return content != null && content.DocumentTypeAlias == "Ads"; } 

    Then in your controller you can check for the existing of those items in ControllerContext.HttpContext.Items["parent"] and ControllerContext.HttpContext.Items["child"] and do what you need to do with pulling the information from the external database.

     

    Hope that helps.

    Andy

Please Sign in or register to post replies

Write your reply to:

Draft