Search code examples
c#asp.net-mvc

Can't redirect error 500 to custom error page in ASP.NET MVC 5


I have been trying to learn how to create a custom error page for my ASP.NET MVC application. I have created a new controller with an action and a new view for this.

public ActionResult Error(int id, string message, string stackTrace)
{
    Response.StatusCode = id;
    ViewBag.Message = message;
    ViewBag.StackTrace = stackTrace;

    return View("~/Views/PaginaErro/PaginaErro.cshtml");
}

And this is the Application_Error method that I have added to the Global.asax.cs:

protected void Application_Error()
{
    Exception exception = Server.GetLastError();

    var httpException = exception as HttpException;

    int httpCode = httpException?.GetHttpCode() ?? (int)HttpStatusCode.InternalServerError;

    Server.ClearError();

    Response.Redirect("~/ErrorPage/Error?id=" + httpCode + "&message=" + HttpUtility.UrlEncode(exception.Message) + "&stackTrace=" + HttpUtility.UrlEncode(exception.StackTrace));
}

Web.config:

<customErrors mode="On" defaultRedirect="~/ErrorPage/Error">
    <error redirect="~/ErrorPage/Error/404" statusCode="404"/>
    <error redirect="~/ErrorPage/Error/500" statusCode="500"/>
</customErrors>

Now, when I receive an error 404, it works perfectly. I hits first the Application_Error method and then after that it goes to the Error action method in the ErrorPageController.

But the issue that I encounter is with the error 500. I can't find what is causing that. Once it reaches the Response.Redirect, it never goes to my ErrorPageController, it keeps looping in the Application_Error.

The exception.Message of my http 500 error is:

The view 'Error' or its master was not found or no view engine supports the searched locations. The following locations were searched:\r\n~/Areas/Erp/Views/AjusteDeEstoque/Error.aspx\r\n~/Areas/Erp/Views/AjusteDeEstoque/Error.ascx\r\n~/Areas/Erp/Views/Shar...

I don't have a view called Error, so I don't know why is it searching for that one.

I'm not sure if this helps, but here is my RouteConfig:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapMvcAttributeRoutes();

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

Solution

  • First off, you should note that the info in web.config in your case is redundant - your Application_Error() function catches any exception and redirects it to the /ErrorPage/Error route.

    I recreated your case and was having issues even entering Application_Error() with the web.config settings - I would encourage you to remove that section and then try again, unless you are adamant on keeping the web.config settings in which case you should change your approach.

    Finally, your redirect will essencially be transmitting the error message and stack trace in the URL, which can potentially be too much information - you should rethink this way of passing information.