Base solution for your next web application
Open Closed

Custom Error 500 Page #2483


User avatar
0
alukaszewski created

I'm having trouble getting my application to show my custom page for Error 500.

I have added a new Action Result in the ErrorController.cs as follows:

using System.Web.Mvc;
using Abp.Auditing;

namespace Callisto.Web.Controllers
{
    public class ErrorController : CallistoControllerBase
    {
        [DisableAuditing]
        public ActionResult E404()
        {
            return View();
        }

        public ActionResult E500()
        {
            return View();
        }
    }
}

and created /Views/Error/E500.cshtml with modifications to show different number/text.

If I browse to "/Error/E500" I see my custom E500.cshtml page OK.

To test Error 500 condition, I temporarily rename the website folder Mpa/Views/Dashboard folder to "Dashboardzz" and this generates an ASPNETZERO 500 error page but does not redirect to the /Error/E500 page.

I have web.config CustomErrors configured:

<customErrors mode="On">
      <error statusCode="404" redirect="~/Error/E404" />
      <error statusCode="500" redirect="~/Error/E500" />
    </customErrors>

...but no redirect seems to occur to "/Error/E500"

Am I missing something?


6 Answer(s)
  • User Avatar
    0
    alukaszewski created

    OK, so I have been reading this blog;

    <a class="postlink" href="http://benfoster.io/blog/aspnet-mvc-custom-error-pages">http://benfoster.io/blog/aspnet-mvc-custom-error-pages</a>

    and I have discovered that outside of the 404 error handling, ASPNETZERO returns the /Shared/error.cshtml - which doesn't look pretty at all (formatting is all messed up as it doesn't use the layout template.)

    I tried looking to see if there was a "HandleErrorAttribute" global filter in my project but I can not find one. How to I get ASPNETZERO to redirect to /Error/E500 when encountering Error 500?

    Thanks, Andy.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    You can define a custom page for each error code in your project. You need to set it in web.config like we did for E404 here <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/blob/9ec982ce90c2767c7c50d5bc0b834f68c2d43feb/src/MyCompanyName.AbpZeroTemplate.Web/Web.config#L71">https://github.com/aspnetzero/aspnet-ze ... config#L71</a>.

  • User Avatar
    0
    alukaszewski created

    <cite>ismcagdas: </cite> Hi,

    You can define a custom page for each error code in your project. You need to set it in web.config like we did for E404 here <a class="postlink" href="https://github.com/aspnetzero/aspnet-zero/blob/9ec982ce90c2767c7c50d5bc0b834f68c2d43feb/src/MyCompanyName.AbpZeroTemplate.Web/Web.config#L71">https://github.com/aspnetzero/aspnet-ze ... config#L71</a>.

    Yes, I have seen and like the Github Error 404 page, however that is NOT within an ASPNETZERO project is it.

    If you care to look at my code samples I posted, I do have the web.config configured with a custom redirect for 500 and the necessary E500.cshtml page and modified ErrorController.cs- but the redirect is not occuring - instead the site loads the /Shared/error.cshtml page. If I go directly to "/Error/E500" then I get my custom error page, so I know the controller and page are fine - it is that the site is not redirecting, and instead loading the contents of the /Shared/error.cshtml page - which looks awful.

  • User Avatar
    0
    alukaszewski created

    P.S. - That is also for Error 404 - not 500, there may be a difference in handling 500 errors in an MVC application? Maybe with a 500 error we cannot go to a view from a controller and it has to be a static page?

  • User Avatar
    0
    alukaszewski created

    OK, So I think I have the desired result by modifying the web.config to include the following in system.webServer :

    <httpErrors errorMode="Custom" existingResponse="Replace">
          <remove statusCode="500" subStatusCode="-1" />
          <error statusCode="500" path="/Error/E500" responseMode="ExecuteURL" />
        </httpErrors>
    

    When a 500 error is encountered, now the page goes to the /Error/E500 page, which is generated by the ErrorController using the _layout.cshtml.

    I understand that potentially, if ASP.NET is having issues itself, then the MVC system may not actually return what I need - but hell, the app would be broken so I'm not sure I care so much!

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Yes, this is the way.