Copied to clipboard

Flag this post as spam?

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


  • Nathaniel Coxall 6 posts 26 karma points
    Sep 24, 2014 @ 16:34
    Nathaniel Coxall
    0

    Returning a 404 after "hijacking" Umbraco Routing

    Hi folks,

    I've got an application running Umbraco 6.1 where parts of the site implement custom MVC controllers AKA hijacked routing.

    However, I'm really struggling to successfully return a custom 404 page from my custom controller.

    I've tried two options so far:

    public class MyCustomController : RenderMvcController
    {
       public override ActionResult Index(RenderModel model)
       {
          if (true) // some business logic fails to find data for this request, return a 404
          {
             // option 1:
             throw new System.Web.HttpException(404, "Not found");
             // option 2:
             return new HttpNotFoundResult();
          }
          // Continue normal processing...
          return CurrentTemplate();
       }
    }
    

    Option 1 returns the standard IIS 404 page, which is fine in a sense (I could configure the "404" error page in web.config) but doesn't work in the context of my site because it's multilingual. I need a different 404 page for each language.

    Option 2 just returns an empty response with 404 headers, which again is fine, but I really want to show the user a helpful message.

    I've been searching around for a couple of days now and just can't come up with a solution. I've tried implementing an IContentFinder class, but that doesn't seem to get called in the context of a "hijacked" route.

    Ideally, I'd like to "return" control back to umbraco so it can return the configured language-specific 404 page that's found in umbracoSettings.config, if that's possible?

    Any help or pointers would be much appreciated!

    Thanks, Nat

  • cheil 10 posts 90 karma points
    May 11, 2016 @ 18:51
    cheil
    0

    Did you ever find a solution to this? I am having the same issue.

  • TheSharpieOne 10 posts 84 karma points
    May 16, 2016 @ 19:04
    TheSharpieOne
    0

    I hate to "+1" this, but I cannot find a solution to this issue myself and I didn't want to open a new thread. I'm using 7.4.3 and cannot figure out how to "return" control back to umbraco after hijacking the route. For me, my route takes some of the parameters and does a lookup, if it doesn't find anything I would like to tell umbraco to act as if the route was never hijacked.

  • Joseph Tossell 1 post 71 karma points
    Jun 14, 2016 @ 16:12
    Joseph Tossell
    0

    This is by no means a documented way of doing it but having a look around I found a handle404 class which Umbraco uses to get the 404 page from the configuration. So I created a little extension method on UmbracoHelper to get the content for me:

    public static IPublishedContent GetErrorPage(this UmbracoHelper h)
    {
        var errorPage = new umbraco.handle404();
        //providing the URL logs a sensible error message
        errorPage.Execute(h.UmbracoContext.HttpContext.Request.Url.PathAndQuery);
        return h.TypedContent(errorPage.redirectID);
    }
    

    Also note that the handle404 class sets the HTTP status code for you.

    Finally to return the error page from a RenderMvcController just do:

    return View("Error", Umbraco.GetErrorPage());
    

    Where "Error" is the name of your error page template.

    As for "act as if the route was never hijacked" does

    return CurrentTemplate(model);
    

    not work?

    If you want to return some different page you could look it up using the UmbracoHelper (i.e. Umbraco.TypedContent(id)) and return this as the model instead.

    If you need to use a different template then return View("template", model)

Please Sign in or register to post replies

Write your reply to:

Draft