locked
Error Page Handling - different scenarios RRS feed

  • Question

  • User747696199 posted

    hi,

    I'm working on a web application that has both MVC and web forms code. There are 3 categories of pages: 1) front end (before log in), 2) subscriber back end, 3) affiliate back end.

    Each has a different Masterpage or _layout.cshtml file; and the UI (navigation menu) is different.

    I got a new requirement to add  "friendly error handling pages" for all of these pages; must have our own css/styling and show a nice message - not 404 error, or server error.

    I've read a bit about the subject; and came across this:

    http://www.asp.net/web-forms/overview/older-versions-getting-started/deploying-web-site-projects/displaying-a-custom-error-page-cs

    As this article was written in 2009, I'm not sure if the approach suggested is still the best for today?

    I'm looking for the simplest way to plug in an error handling page, that can show a friendly message + using our own styling.

    Will it be coding + IIS config? we are using IIS7.5.

    ANy suggestion is appreciated.

    Thanks,

    Claudia

    Monday, August 17, 2015 12:22 AM

Answers

  • User-656509064 posted

    Hi Claudia,

    I'm sure there are several ways. I simply do like the following example in Web.config.

    <system.web>
        <customErrors mode="RemoteOnly">
            <error statusCode="500" redirect="~/Errors/InternalServer" />
            <error statusCode="404" redirect="~/Error/NotFound" />
        </customErrors>
    </system.web>

    Then I have ErrorsController where I handle different scenarios to cater for the above. From here you can have your views use whatever layouts and CSS you like.

    I like to keep these things simple and the above is simple enough to work for me. Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 17, 2015 7:43 PM

All replies

  • User-656509064 posted

    Hi Claudia,

    I'm sure there are several ways. I simply do like the following example in Web.config.

    <system.web>
        <customErrors mode="RemoteOnly">
            <error statusCode="500" redirect="~/Errors/InternalServer" />
            <error statusCode="404" redirect="~/Error/NotFound" />
        </customErrors>
    </system.web>

    Then I have ErrorsController where I handle different scenarios to cater for the above. From here you can have your views use whatever layouts and CSS you like.

    I like to keep these things simple and the above is simple enough to work for me. Smile

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 17, 2015 7:43 PM
  • User-1454326058 posted

    Hi Claudia,

    The customErrors section in web.config is apply to current site, if you also need to capture IIS level error, you could configure in httpErrors section in web.config.

    # HTTP Errors <httpErrors>

    https://www.iis.net/configreference/system.webserver/httperrors

    Regards

    Starain

    Thursday, August 20, 2015 3:24 AM
  • User747696199 posted

    HI Starain,

    I have a follow-up question:

    Can I indicate for several Controllers, all the .cshtml files associated with this COntroller will go to a different error page (no. 2)?

    Is it possible?

    Or - if an user is authenticated, then go to a different error page (no.3) than the normal error page?

    Thanks,

    Claudia

    Monday, August 24, 2015 10:43 AM
  • User-1454326058 posted

    Hi Claudia,

    You could custom error handle, then you could override OnException method and do other logical.

    There are some links that can help you:

    # Exception or Error Handling and Logging in MVC

    http://www.dotnet-tricks.com/Tutorial/mvc/19D9140313-Exception-or-Error-Handling-and-Logging-in-MVC4.html

    http://stackoverflow.com/questions/8144695/asp-net-mvc-custom-handleerror-filter-specify-view-based-on-exception-type

    Regards

    Starain

    Monday, August 24, 2015 11:03 PM
  • User747696199 posted

    Hi Starain,

    Update - I followed the 1st link you provided; it worked for 3 controllers that I want to add a custom error page. However, after I added this to web.config:

    <customErrors mode="On" defaultRedirect="Error.htm"/>

    It caused an existing URL redirect rule to NOT work; which is for all 404 error, it will redirect to a certain page.

    Now, after adding the above line of code in the web.config, all 404 errors will display the Error.htm page, not following the URL redirect rule.

    How to make BOTH situations work?

    Please advise.

    Thanks,

    Claudia

    Friday, September 11, 2015 12:09 AM
  • User-1454326058 posted

    Hi Claudia,

    I don’t understand clearly. Could you provide details with sample?

    Regards

    Starain

    Wednesday, September 16, 2015 5:25 AM