Copied to clipboard

Flag this post as spam?

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


  • Dmitriy 168 posts 588 karma points
    Apr 03, 2019 @ 15:42
    Dmitriy
    0

    Route Hijacking with builtin 404 handling

    Hello, Umbracians

    Here is my code:

    public class RedirectionPageController : RenderMvcController
    {
        public ActionResult Index(RenderModel model)
        {
            var child = model.Content.FirstChild(); 
            var viewName = child.GetTemplateAlias();
    
            if(viewName.IsNullOrWhiteSpace())
            {
                return new HttpStatusCodeResult(HttpStatusCode.NotFound);
            }
    
            return View(viewName, child); 
        }
    
    }
    

    I want to render view of first child of that page, but if it have no template - render the 404 page. But builtin Umbraco 404-handling behavior does't executed in case. All the others pages (without hijacking) without views (templates) working well with builtin 404-handling.

    How to make Umbraco force it own 404 handling?

  • Dennis Flæng Jørgensen 35 posts 145 karma points c-trib
    Apr 03, 2019 @ 17:24
    Dennis Flæng Jørgensen
    1

    I think you need to use the IContentFinder instead if you want the default 404-handling to be in effect.

    https://our.umbraco.com/documentation/reference/routing/request-pipeline/icontentfinder

  • Dennis Flæng Jørgensen 35 posts 145 karma points c-trib
    Apr 03, 2019 @ 17:46
    Dennis Flæng Jørgensen
    2

    I made a quick example for you tested with Umbraco 8:

    public class MyApplication : IUserComposer
    {
        public void Compose(Composition composition)
        {
            composition.ContentFinders().Replace<ContentFinderByUrl, RedirectionContentFinder>();
        }
    }
    
    public class RedirectionContentFinder : ContentFinderByUrl
    {
        public RedirectionContentFinder(ILogger logger) : base(logger)
        {
        }
    
        public override bool TryFindContent(PublishedRequest request)
        {
            bool result = base.TryFindContent(request);
            if (request.PublishedContent != null && request.PublishedContent.ContentType.Alias.Equals("redirectionPage", StringComparison.InvariantCultureIgnoreCase))
            {
                var firstChild = request.PublishedContent.Children.FirstOrDefault();
                var firstChildTemplateAlias = firstChild?.GetTemplateAlias();
                if (firstChildTemplateAlias.IsNullOrWhiteSpace())
                {
                    request.PublishedContent = null;
                    request.Is404 = true;
                    return false;
                }
                request.PublishedContent = firstChild;
                return true;
            }
            return result;
        }
    }
    

    For Umbraco 7 you should replace IUserComposer with IApplicationEventHandler and put the code in ApplicationStarting

  • Dmitriy 168 posts 588 karma points
    Apr 04, 2019 @ 12:20
    Dmitriy
    0

    Hi, Dennis

    A good solution, but not what I need.

    I don't want to make my own ContentFinder, I need to use the builtin implementation of it.

    Are there any other approaches for?

    I think I have to get ContentFinder collection and find LastChanceContentFinder or something like that.

    Thanks

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies