I'm developing a MVC3 base website and I am looking for a solution for handling errors and Render custom Views for each kind of error. So imagine that I have a "Error" Controller where his main action is "Index" (generic error page) and this controller will have a couple more actions for the errors that may appear to the user like "Handle500" or "HandleActionNotFound".
So every error that may happen on the website may be handled by this "Error" Controller (examples: "Controller" or "Action" not found, 500, 404, dbException, etc).
I am using Sitemap file to define website paths (and not route).
This question was already answered, this is a reply to Gweebz
My final applicaiton_error method is the following:
protected void Application_Error() {
//while my project is running in debug mode
if (HttpContext.Current.IsDebuggingEnabled && WebConfigurationManager.AppSettings["EnableCustomErrorPage"].Equals("false"))
{
Log.Logger.Error("unhandled exception: ", Server.GetLastError());
}
else
{
try
{
var exception = Server.GetLastError();
Log.Logger.Error("unhandled exception: ", exception);
Response.Clear();
Server.ClearError();
var routeData = new RouteData();
routeData.Values["controller"] = "Errors";
routeData.Values["action"] = "General";
routeData.Values["exception"] = exception;
IController errorsController = new ErrorsController();
var rc = new RequestContext(new HttpContextWrapper(Context), routeData);
errorsController.Execute(rc);
}
catch (Exception e)
{
//if Error controller failed for same reason, we will display static HTML error page
Log.Logger.Fatal("failed to display error page, fallback to HTML error: ", e);
Response.TransmitFile("~/error.html");
}
}
}
I see you added a config value for
EnableCustomErrorPage
and you're also checkingIsDebuggingEnabled
to determine whether or not to run your error handling.Since there's already a
<customErrors/>
configuration in ASP.NET (which is meant exactly for this purpose) it's easiest to just say :Then in the config you'd put
<customErrors mode="RemoteOnly" />
which is safe to deploy like that, and when you need to test your custom error page you'd set it to<customErrors mode="On" />
so you can verify that it works.Note you also need to check if
HttpContext.Current
is null because an exception inApplication_Start
will still his this method although there won't be an active context.I'm using MVC 4.5 and I was having issues with Darin's solution. Note: Darin's solution is excellent and I used it to come up with my solution. Here's my modified solution:
You can also do this in the Web.Config File. Here is an example that works in IIS 7.5.
You can display a user-friendly error page with the correct http status code by implementing Jeff Atwood's User Friendly Exception Handling module with a slight modification for the http status code. It works without any redirects. Although the code is from 2004(!), it works well with MVC. It can be configured entirely in your web.config, with no MVC project source code changes at all.
The modification required to return the original HTTP status rather than a
200
status is described in this related forum post.Basically, in Handler.vb, you can add something like:
Here is more articles How to create custom error pages with MVC http://kitsula.com/Article/MVC-Custom-Error-Pages.
Here's an example of how I handle custom errors. I define an
ErrorsController
with actions handling different HTTP errors:and then I subscribe for the
Application_Error
inGlobal.asax
and invoke this controller: