I'm trying to handle global(StatusCode 500) and http exception in Asp.Net MVC Using Custom Error Page with Proper HTTP status code using Application_Error() event in Global.asax file. To make it simple actually I have only the same view error for all types of handled exception.
Now the problem is when a 500 exception is handled it goes a first time in the else section of the Application_Error and then it goes back to the if section and finally return 404 http exception instead of 500 exception.
How I can fix that by stop running after it goes in the else section of Application_Error when a 500 exception is handled ?
This is my configuration : web.config
<system.web>
<customErrors mode="Off"/>
</system.web>
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="400"/>
<remove statusCode="401"/>
<remove statusCode="403"/>
<remove statusCode="404"/>
<remove statusCode="500"/>
<error statusCode="400" responseMode="ExecuteURL" path="/Error"/>
<error statusCode="401" responseMode="ExecuteURL" path="/Error"/>
<error statusCode="403" responseMode="ExecuteURL" path="/Error"/>
<error statusCode="404" responseMode="ExecuteURL" path="/Error"/>
<error statusCode="500" responseMode="ExecuteURL" path="/Error"/>
</httpErrors>
</system.webServer>
Method Application_Error():
public void Application_Error(object sender, EventArgs e)
{
Exception exception = Server.GetLastError();
HttpException httpException = exception as HttpException;
// Clear the response stream
HttpContext httpContext = ((HttpApplication)sender).Context;
httpContext.Response.Clear();
httpContext.ClearError();
var routeData = new RouteData();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "Error");
routeData.Values.Add("exception", exception);
if (exception.GetType() == typeof(HttpException)) //It's an Http Exception
{
routeData.Values.Add("statusCode", ((HttpException)exception).GetHttpCode());
switch (routeData.Values["statusCode"])
{
case 400:
routeData.Values.Add("status", "400 - Bad Request");
break;
case 401:
routeData.Values.Add("status", "401 - Access Denied");
break;
case 403:
routeData.Values.Add("status", "403 - Forbidden");
break;
case 404:
routeData.Values.Add("status", "404 - Page Not Found");
break;
case 500:
default:
routeData.Values.Add("status", "500 - Internal Server Error");
break;
}
}
else
{
routeData.Values.Add("statusCode", 500);
routeData.Values.Add("status", "500 - Internal Server Error");
}
// Avoid IIS7 getting in the middle
httpContext.Response.TrySkipIisCustomErrors = true;
// Call target Controller and pass the routeData.
IController controller = new ErrorController();
controller.Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));
Response.End();
// clear error on server
//Server.ClearError();
}
Controller ErrorController :
public class ErrorController : Controller
{
// GET: /Error/
public ActionResult Error(int statusCode, string status, Exception exception)
{
Response.StatusCode = statusCode;
ViewBag.StatusCode = statusCode;
ViewBag.Status = status;
return View();
}
}