I have code in the global.asax
file's Application_Error
event which executes when an error occurs and emails details of the error to myself.
void Application_Error(object sender, EventArgs e)
{
var error = Server.GetLastError();
if (error.Message != "Not Found")
{
// Send email here...
}
}
This works fine when I'm running it in Visual Studio, however when I publish to our live server the Application_Error
event does not fire.
After some testing I can get the Application_Error
firing when I set customErrors="Off"
, however setting it back to customErrors="On"
stops the event from firing again.
Can anyone suggest why Application_Error
would not be firing when customErrors
are enabled in the web.config
?
I found an article which describes a much cleaner way of making custom error pages in an MVC3 web app which does not prevent the ability to log the exceptions.
The solution is to use the
<httpErrors>
element of the<system.webServer>
section.I configured my Web.config like so...
I also configured
customErrors
to havemode="Off"
(as suggested by the article).That makes the responses overriden by an ErrorController's actions. Here is that controller:
The views are very straight forward, I just used standard Razor syntax to create the pages.
That alone should be enough for you to use custom error pages with MVC.
I also needed logging of Exceptions so I stole the Mark's solution of using a custom ExceptionFilter...
The last thing you need to so is register the Exception Filter in your Global.asax.cs file:
This feels like a much cleaner solution than my previous answer and works just as well as far as I can tell. I like it especially because it didn't feel like I was fighting against the MVC framework; this solution actually leverages it!
In case of ASP.NET MVC5 use
You can find it in
FilterConfig.cs
ofApp_Start
folder.This blog entry helped me:
http://asp-net.vexedlogic.com/2011/04/23/asp-net-maximum-request-length-exceeded/
If you're using IIS 7.0 or higher, you can alter your Web.config file to handle requests that are too large. There are some caveats, but here is an example:
There are additional details about these config file elements here:
http://www.iis.net/ConfigReference/system.webServer/security/requestFiltering/requestLimits
Status code 404.13 is defined as "Content Length Too Large". One important thing to note is that the
maxAllowedContentLength
is specified in bytes. This is different from themaxRequestLength
setting you find in the<system.web>
section, which is specified in kilobytes.Also note that the
path
attribute must be an absolute path when theresponseMode
isRedirect
, so prepend the virtual directory name, if relevant. Jesse Webb's informative answers show how to do this withresponseMode="ExecuteURL"
, and I would think that approach would work well, too.This approach does not work if you're developing using the Visual Studio Development Server (Cassini, the Web server integrated into Visual Studio). I'm assuming it would work in IIS Express, but I haven't tested that.