Copied to clipboard

Flag this post as spam?

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


  • Markus 33 posts 58 karma points
    Aug 09, 2012 @ 11:25
    Markus
    0

    Display 404 page from razor code

    I have a template which displays a page list items, and uses a modified version of the paging script coming with Umbraco.

    The script works by retrieving the current page to display from a parameter in the url, typically ?page=xx

    It is possible to call this url with invalid page numbers, and I'd like to return the 404 page for those requests.

    How can I display the 404 page from within razor code? I don't want to redirect the user, just display the 404 page.

    I tried

    throw new HttpException(404, "Page not found");

    but this results in macro errors.

  • gilad 185 posts 425 karma points
    Aug 09, 2012 @ 11:47
    gilad
    0

    Hii Markus

    You can do something like this : 

    dynamic errorNode = new DynamicNode(404PageId);
    Response.Redirect(errorNode.Url);

    Hope that help.

    Cheers.

  • Markus 33 posts 58 karma points
    Aug 09, 2012 @ 12:35
    Markus
    0

    Hi gilad, thanks for the suggestion. But wouldn't that redirect the user to the 404 page? I'd really like to just display the 404 page without redirecting.

  • Funka! 398 posts 661 karma points
    Aug 10, 2012 @ 00:10
    Funka!
    1

    I believe if you have CustomErrors enabled in your web.config, all you need to do is call the following:

    Response.StatusCode = 404;
    Response.StatusDescription = "Not Found";

    At this point, IIS should take over and display the proper 404 page. (And as a side note, if you did NOT want IIS to take over, you would precede these lines above with Response.TrySkipIisCustomErrors = true;

    Best of luck!

  • Markus 33 posts 58 karma points
    Aug 10, 2012 @ 14:01
    Markus
    0

    Hi Funka!

    I tried your suggestion, and it worked as soon as I added the following to the web.config (obviously...):

        <httpErrors errorMode="Custom" existingResponse="Replace" defaultResponseMode="ExecuteURL">
          <remove statusCode="404"  />
          <error statusCode="404" path="/404" responseMode="ExecuteURL" />
        </httpErrors>

    Thanks!

  • Funka! 398 posts 661 karma points
    Aug 10, 2012 @ 21:28
    Funka!
    0

    Excellent, glad you got it working!

    Just be careful using the path "/404/" if you are using extensionless (directory) URLs, as this bug here may cause additional frustration. (It did for me! I now use "/error404" for my own.)

  • denisedelbando 141 posts 339 karma points
    Jan 11, 2016 @ 15:18
    denisedelbando
    0

    is there another way of doing this if we have: https://our.umbraco.org/wiki/install-and-setup/configuring-404-pages in our web.config?

    I cannot find a proper way of handling 404 without changing the web.config. this is already in production and if i change the PassThrough value i would have to test the entire site again.

  • Mark Bowser 273 posts 860 karma points c-trib
    Nov 30, 2016 @ 18:32
    Mark Bowser
    0

    I'm trying to figure out how to make this work without using the customErrors in the web.config as well. I want to use the error404 settings in the /config/umbracoSettings.config. Is there a way to throw an HttpException and have umbraco kick in and send me to the culturally appropriate 404 page as configured in the /config/umbracoSettings.config?

    For example, what if I wanted to put something like this in a view?

    @inherits UmbracoTemplatePage
    
    // logic here to get some values from query string
    //...
    
    if(myVal == null)
    {
        throw new HttpException(404, "Page not found");
    }
    

    This is what I have in my /config/umbracoSettings.config that I want to be able to leverage:

    <errors>
      <error404>
        <errorPage culture="default">1620</errorPage>
        <errorPage culture="en-US">1620</errorPage>
        <errorPage culture="en-GB">15014</errorPage>
        <errorPage culture="en-AU">17617</errorPage>
        <errorPage culture="de">3810</errorPage>
        <errorPage culture="fr-FR">3509</errorPage>
        <errorPage culture="ja-JP">10095</errorPage>
        <errorPage culture="zh-CN">9599</errorPage>
        <errorPage culture="es-MX">10591</errorPage>
        <errorPage culture="pt-BR">11084</errorPage>
      </error404>  
    </errors>
    

    Do I have to make one of those custom ContentFinders to get this working? I'm guessing that's how umbraco is implementing its error404 functionality. Is that right?

  • Alex Skrypnyk 6163 posts 24143 karma points MVP 8x admin c-trib
    Feb 27, 2020 @ 16:03
    Alex Skrypnyk
    0

    Hi Mark Bowser

    Did you find a way to do it? I really need the same functionality

    Alex

Please Sign in or register to post replies

Write your reply to:

Draft