'The remote host closed the connection' when redirecting from application_error

0

I'm trying to test a simple error catch in my application.

In my BaseController:

protected override void OnException(ExceptionContext filterContext)
{
    if (filterContext.ExceptionHandled)
    {
        return;
    }

    var logger = new Logger();
    logger.LogError(filterContext.Exception, ControllerContext.RouteData.Values["controller"].ToString());

    filterContext.Result = new ViewResult
    {
        ViewName = "~/Views/Shared/Error.cshtml",
        ViewData = new ViewDataDictionary()
        {
            {"exception", filterContext.Exception}
        }
    };
    filterContext.ExceptionHandled = true;
}

This works great, in that it logs the error, then redirects to an errror page, displaying the exception message. Fantastic.

But what if an error occurs in that action?

I added this line:

throw new System.Exception("aasdas");

and I have this in my global.asax.cs:

protected void Application_Error(Object sender, EventArgs e)
{
    var logger = new Logger();
    var ex = Server.GetLastError();
    logger.LogError(ex, "global");
    Server.ClearError();

    Response.Redirect("~/Views/Shared/Error.cshtml");
}

However, when it tries to Reponse.Redirect I get this error

System.Web.HttpException: 'The remote host closed the connection. The error code is 0x80070057.'

How am I supposed to handle this type of error? Why can't I redirect from Application_Error?

I just noticed that if I just continue debugging (F5) then I get the error, the requests fires again, and this time it works properly, but that means the error gets logged twice ... What..

c#
asp.net-mvc
redirect
error-handling
asked on Stack Overflow Apr 23, 2018 by Bassie • edited Apr 23, 2018 by Bassie

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0