Copied to clipboard

Flag this post as spam?

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


  • Ayo Adesina 430 posts 1023 karma points
    Aug 30, 2017 @ 11:39
    Ayo Adesina
    0

    Custom Routing and IContentFinder

    I'm building a website that is a SPA and I want to use Umbraco as a back end.

    The sites content is currently coming from static JSON files, I want Umbraco to replace these JSON files by genrating the JSON based on what is in the CMS.

    As a first step I have put the website with its static files in to an Umbraco Installation. Created a home node with a template.

    This is working correctly. I have also aranged all the content for the JSON file in to logical nodes in the Umbraco content tree.

    Take these URLs for an example

    /data/nl/news.json
    
    /data/en/news.json
    

    How can I map BOTH of these URLs to a specific content node in Umbraco?

    In my case I have:

    -Home (Template with SPA)
      -News (THIS IS THE NODE I WANT THE URLS TO MAP TO)
          -NewsItem
          -NewsItem
    

    When the request hits that node I want to read the URL and then go and get the correct content from the children and return it as JSON.

    Not 100% sure if I should be trying to set up some custom routes so the URL's hit a controller, or the IContentFinder, or possibly a combination of both.

    Any ideas?

  • Ayo Adesina 430 posts 1023 karma points
    Aug 30, 2017 @ 14:59
    Ayo Adesina
    0

    No replies :-( is my question clear enough?

  • Nicholas Westby 2054 posts 7100 karma points c-trib
    Aug 30, 2017 @ 15:12
    Nicholas Westby
    0

    Might be easier to do with a query string. That's what we do for Jacuzzi: https://www.jacuzzi.com/en-us/hot-tubs?spa=true

    Then you don't need a custom URL provider and content finder. We set the default controller and have an action method that looks like this (some parts were simplified since they aren't relevant to your question):

    {
    
        // Variables.
        var page = MappingUtility.MapTypicalPage(CurrentPage, Umbraco, UmbracoContext);
        var isSpa = Request.QueryString["spa"] == "true";
    
        // Normal page or single-page app page?
        if (isSpa)
        {
    
            // For some unknown reason, Umbraco was returning a 404 when this action method
            // returned JSON. This fixes that by forcing a 200 response.
            Response.StatusCode = 200;
    
            // Return JSON.
            var data = PageDataUtility.GetPageData(page, Request);
            return Json(data, JsonRequestBehavior.AllowGet);
    
        }
        else
        {
            return CurrentTemplate(page);
        }
    
    }
    

    You can read about how to set a default controller here: https://our.umbraco.org/documentation/reference/routing/custom-controllers#change-the-default-controller

    I assume you've already managed some way of creating URL's that contain the language, so I won't go into how we did that for Jacuzzi.

  • Ayo Adesina 430 posts 1023 karma points
    Aug 31, 2017 @ 12:27
    Ayo Adesina
    101

    I solved this problem in the end by using URL rewriting.

    Incase someone else has this problem:

    1. If you don't have it installed already install the IIS rewrite module. https://www.microsoft.com/en-us/download/details.aspx?id=47337

    2. Add a rewrite rule to your webconfig (I guess you can do this at IIS level too) so inside my WebConfig file the rule looks like this.

       <rewrite><rules>
      <rule name="NL-News" stopProcessing="true">
        <match url="^data/nl/news.json" ignoreCase="true" />
        <action type="Redirect" url="newspage?culture=nl-NL" />
      </rule>
      <rule name="EN-News" stopProcessing="true">
        <match url="^data/en/news.json" ignoreCase="true" />
        <action type="Redirect" url="newspage?culture=en-US" />
      </rule>
      

    Job done, hope this helps someone else.

Please Sign in or register to post replies

Write your reply to:

Draft