In my ASP.NET web application, I have defined custom error pages in my web.config file as follows:
I think you'd instead implement/use the Application_Error event in Global.asax, and do your processing/redirects there.
Providing you call Server.ClearError in that handler, I don't think it will use the customErrors config at all.
In the global.asax, catch the 404 error and redirect to the file not found page. I didn't require the aspxerrorpath and it worked a treat for me.
void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 404)
{
Response.Redirect("~/filenotfound.aspx");
}
else
{
// your global error handling here!
}
}
In my case, i prefer not use Web.config. Then i created code above in Global.asax file:
protected void Application_Error(object sender, EventArgs e)
{
Exception ex = Server.GetLastError();
//Not Found (When user digit unexisting url)
if(ex is HttpException && ((HttpException)ex).GetHttpCode() == 404)
{
HttpContextWrapper contextWrapper = new HttpContextWrapper(this.Context);
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "NotFound");
IController controller = new ErrorController();
RequestContext requestContext = new RequestContext(contextWrapper, routeData);
controller.Execute(requestContext);
Response.End();
}
else //Unhandled Errors from aplication
{
ErrorLogService.LogError(ex);
HttpContextWrapper contextWrapper = new HttpContextWrapper(this.Context);
RouteData routeData = new RouteData();
routeData.Values.Add("controller", "Error");
routeData.Values.Add("action", "Index");
IController controller = new ErrorController();
RequestContext requestContext = new RequestContext(contextWrapper, routeData);
controller.Execute(requestContext);
Response.End();
}
}
And thtat is my ErrorController.cs
public class ErrorController : Controller
{
// GET: Error
public ViewResult Index()
{
Response.StatusCode = 500;
Exception ex = Server.GetLastError();
return View("~/Views/Shared/SAAS/Error.cshtml", ex);
}
public ViewResult NotFound()
{
Response.StatusCode = 404;
return View("~/Views/Shared/SAAS/NotFound.cshtml");
}
}
And that is my ErrorLogService.cs
//common service to be used for logging errors
public static class ErrorLogService
{
public static void LogError(Exception ex)
{
//Do what you want here, save log in database, send email to police station
}
}
If you remove aspxerrorpath=/ and you use response redirect during error handling you'll get exception there will be redirection loop.
The best solution (more a workaround..) I implemented since now to prevent aspxerrorpath
issue continuing to use ASP.NET CustomErrors support, is redirect to the action that implements Error handling.
These are some step of my solution in an ASP.NET MVC web app context:
First enable custom errors module in web.config
<customErrors mode="On" defaultRedirect="~/error/500">
<error statusCode="404" redirect="~/error/404"/>
</customErrors>
Then define a routing rule:
routes.MapRoute(
name: "Error",
url: "error/{errorType}/{aspxerrorpath}",
defaults: new { controller = "Home", action = "Error", errorType = 500, aspxerrorpath = UrlParameter.Optional },
);
Finally implement following action (and related views..):
public ActionResult Error(int errorType, string aspxerrorpath)
{
if (!string.IsNullOrEmpty(aspxerrorpath)) {
return RedirectToRoute("Error", errorType);
}
switch (errorType) {
case 404:
return View("~/Views/Shared/Errors/404.cshtml");
case 500:
default:
return View("~/Views/Shared/Errors/500.cshtml");
}
}