Copied to clipboard

Flag this post as spam?

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


  • Bo Jacobsen 606 posts 2404 karma points
    Feb 02, 2017 @ 17:55
    Bo Jacobsen
    0

    Return 404 after hijacking umbraco route

    Hello Everyone.

    There is alot of this going on in the forum. But i cant find any answers that solves my situation.

    using Umbraco 7.5.8

    I want to return the error page i am setting in umbracoSettings.config in my RenderMvcController. And i want to do so whitout changing the url.

    First approach:

    Returns errorPage.redirectID is null. I guess thats because i need to make a INotFoundHandler?

    Second approach:

    Just return the current content from the RenderModel passed to it.

    public override ActionResult Index(RenderModel model)
            {
                // I only wanna return the 404 page when something is not right..
                if (someConditionsAreNotMeet)
                {
                    // I dunno if i need this.
                    Response.TrySkipIisCustomErrors = true;
                    Response.StatusCode = 404;
                    Response.Status = "404 Not Found";
    
                    // First approach
                    var errorPage = new umbraco.handle404();
                    errorPage.Execute(Request.Url.PathAndQuery);
                    var typedErrorPage = Umbraco.TypedContent(errorPage.redirectID);
                    model = new RenderModel(typedErrorPage);
    
                    // Second approach (1136 is the id of my 404 page).
                    var typedErrorPage = Umbraco.TypedContent(1136);
                    model = new RenderModel(typedErrorPage);
                }
    
                return base.Index(model);
            }
    

    Any pointers would be appreciated.

  • Bo Jacobsen 606 posts 2404 karma points
    Feb 03, 2017 @ 14:39
    Bo Jacobsen
    100

    I solved it by doing this.

    public override ActionResult Index(RenderModel model) 
    {
        if (someConditionsAreNotMeet)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.StatusCode = 400;
            System.Web.HttpContext.Current.Response.Status = "404 Page Not Found";
    
            IPublishedContent c = Umbraco.TypedContent(1136);
            RenderModel rm = new RenderModel(c);
    
            return View(rm.Content.GetTemplateAlias(), rm);
        }
    
        return base.Index(model);
    }
    

    if you want to grab your umbracoSettings.config errorList you can do this:

    using Umbraco.Core.Configuration;
    using Umbraco.Core.Configuration.UmbracoSettings;
    
    IEnumerable<IContentErrorPage> umbracoSettingsErrorPages = UmbracoConfig.For.UmbracoSettings().Content.Error404Collection;
    foreach (IContentErrorPage umbracoSettingsErrorPage in umbracoSettingsErrorPages.Where(x => x.HasContentId))
    {
        var errorPageId = umbracoSettingsErrorPage.ContentId;
        var cultureInfoName = umbracoSettingsErrorPage.Culture;         
    }
    

    The logic for it, is for to you ;)

Please Sign in or register to post replies

Write your reply to:

Draft