Copied to clipboard

Flag this post as spam?

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


  • Philip Hayton 98 posts 435 karma points
    Apr 27, 2019 @ 10:32
    Philip Hayton
    0

    Identify redirections when using GetByRoute

    Hi guys,

    I have an API controller that serializes Content nodes to json. I'm using the ContentCache to lookup the content by Url, which works as intended.

    However, when nodes are renamed and thus a redirection is created, I'd like to return a 301 instead for the old URLs. The problem is the ContentCache doesn't seem to recognize redirections.

    Does anybody have any suggestions on how I might be able to identify redirected URLs and return a 301? Do I need to implement a custom ContentFindER? Or am I over thinking it?

    Any help is greatly appreciated! For info, here is my controller action;

        [HttpGet]
        [Route("get/?url={url}")]
        public object Get(string url)
        {
            var content = UmbracoContext.ContentCache.GetByRoute(url);
            if(content == null) throw new HttpResponseException(HttpStatusCode.NotFound);
            return new ContentDataModel(content);
        }
    
  • Marc Goodson 2136 posts 14297 karma points MVP 8x c-trib
    Apr 27, 2019 @ 11:08
    Marc Goodson
    100

    Hi Philip

    The way the Redirect Url Management functionality works is the redirect is stored in a separate database table to the Umbraco Cache, and the Cache takes precedent, so if a route isn't in the Umbraco Cache, then just before serving a 404, Umbraco will look to see if a redirect exists in the umbracoRedirectUrl database table. So I think in your scenario you can just do the same eg:

       [HttpGet]
            [Route("get/?url={url}")]
            public object Get(string url)
            {
                var content = UmbracoContext.ContentCache.GetByRoute(url);
                if(content == null) {
                     //no matching published Umbraco content, let's see if a redirect exists:
                    // Umbraco has a core RedirectUrlService that queries the db with a snappily name method called GetModeRecentRedirectUrl...
    
                     IRedirectUrl redirectUrl = Services.RedirectUrlService.GetMostRecentRedirectUrl(url);
                     // (now I'm not sure what format your url is in, but this method is expecting an Umbraco route, which if you have domains set will need the prefix of the route domain id see: https://github.com/umbraco/Umbraco-CMS/blob/v7/dev/src/Umbraco.Web/Routing/ContentFinderByRedirectUrl.cs#L23 for an example)
    
                      if (redirectUrl!=null){
                             // we have a redirect!
                             var targetRedirectId =  redirectUrl.ContentId;
                             content = UmbracoContext.ContentCache.GetById(targetRedirectId);
                             if (content == null){
                                //the redirect content doesn't exist!
                                throw new HttpResponseException(HttpStatusCode.NotFound);
                              }
                     }
                     else {
                           // no redirect, no content, return not found
                           throw new HttpResponseException(HttpStatusCode.NotFound);
                    }
             }
                //somehow we have content!
                return new ContentDataModel(content);
            }
    

    Anyway that's the gist if I've understood correctly!

    regards

    Marc

  • Philip Hayton 98 posts 435 karma points
    Apr 27, 2019 @ 20:22
    Philip Hayton
    0

    Hi Marc,

    Ah I never even thought to look in Services! Your answer is bang on the money though, it works a treat.

    Thanks for the swift response + knowledge on how it works. Really appreciate it amigo. #h5yr

    Kind regards

    Phil

Please Sign in or register to post replies

Write your reply to:

Draft