Copied to clipboard

Flag this post as spam?

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


  • Stefan Kip 1614 posts 4131 karma points c-trib
    Feb 08, 2016 @ 10:39
    Stefan Kip
    0

    Custom error handlers

    I'm trying to implement custom error handlers in Umbraco with MVC, based on this topic on SO.
    What I've got is the following:

    public class Global : Umbraco.Web.UmbracoApplication
    {
        protected void Application_EndRequest()
        {
            if (Context.Response.StatusCode == 404)
            {
                Response.Clear();
    
                var contextBase = new HttpContextWrapper(HttpContext.Current);
                using (var context = UmbracoContext.EnsureContext(contextBase, ApplicationContext.Current, new WebSecurity(contextBase, ApplicationContext.Current), UmbracoConfig.For.UmbracoSettings(), UrlProviderResolver.Current.Providers, false))
                {
                    var publishedContent = UmbracoContext.Current.ContentCache.GetByRoute("/404");
    
                    // Todo: change the current request routing to the 404 page
                }
            }
        }
    }

    The next step would be to change the current request/routing to show the /404 page. I'm ensuring the UmbracoContext because I'm requesting a static URL in this case ('/somename.jpg').

    Any hints on how to do this?

  • Stephen 767 posts 2273 karma points c-trib
    Feb 08, 2016 @ 10:53
    Stephen
    0

    Might be wrong, but in Application_EndRequest it is too late for Umbraco to render a page, so your best bet would be a redirect.

    (ok, that might be a quick answer... need to look into more details)

  • Stefan Kip 1614 posts 4131 karma points c-trib
    Feb 08, 2016 @ 10:58
    Stefan Kip
    0

    That would be a shame. This works, but isn't based on a page in Umbraco, just a controller and a view without CMS data:

    public class Global : Umbraco.Web.UmbracoApplication
    {
        protected void Application_EndRequest()
        {
            if (Context.Response.StatusCode == 404)
            {
                Response.Clear();
    
                var rd = new RouteData();
                rd.Values["controller"] = "Errors";
                rd.Values["action"] = "NotFound";
    
                var contextBase = new HttpContextWrapper(HttpContext.Current);
                using (var context = UmbracoContext.EnsureContext(contextBase, ApplicationContext.Current, new WebSecurity(contextBase, ApplicationContext.Current), UmbracoConfig.For.UmbracoSettings(), UrlProviderResolver.Current.Providers, false))
                {
                    IController c = new ErrorsController();
                    c.Execute(new RequestContext(new HttpContextWrapper(Context), rd));
                }
            }
        }
    }
    

    In my previous solution I did a WebRequest to '/404' and wrote the result to the current Response, but I would really like to do it without WebRequest this time.

  • 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