Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>UPDATE</strong><br> Since this answer does provide a solution, I will not edit it, but I have found a much cleaner way of solving this problem. See <a href="https://stackoverflow.com/a/9572858/346561">my other answer</a> for details...</p> <p><strong>Original Answer:</strong><br> I figured out why the <code>Application_Error()</code> method is not being invoked...</p> <p><strong>Global.asax.cs</strong></p> <pre><code>public class MvcApplication : System.Web.HttpApplication { public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); // this line is the culprit } ... } </code></pre> <p>By default (when a new project is generated), an MVC application has some logic in the <code>Global.asax.cs</code> file. This logic is used for mapping routes and registering filters. By default, it only registers one filter: a <code>HandleErrorAttribute</code> filter. When customErrors are on (or through remote requests when it is set to RemoteOnly), the HandleErrorAttribute tells MVC to look for an Error view and it never calls the <code>Application_Error()</code> method. I couldn't find documentation of this but it is explained in <a href="https://softwareengineering.stackexchange.com/questions/45195/what-are-the-definitive-guidelines-for-custom-error-handling-in-asp-net-mvc-3/45197#45197">this answer on programmers.stackexchange.com</a>.</p> <p>To get the ApplicationError() method called for <strong>every</strong> unhandled exception, simple remove the line which registers the HandleErrorAttribute filter.</p> <p>Now the problem is: How to configure the customErrors to get what you want...</p> <p>The customErrors section defaults to <code>redirectMode="ResponseRedirect"</code>. You can specify the defaultRedirect attribute to be a MVC route too. I created an ErrorController which was very simple and changed my web.config to look like this...</p> <p><strong>web.config</strong></p> <pre><code>&lt;customErrors mode="RemoteOnly" redirectMode="ResponseRedirect" defaultRedirect="~/Error"&gt; &lt;error statusCode="404" redirect="~/Error/PageNotFound" /&gt; &lt;/customErrors&gt; </code></pre> <p>The problem with this solution is that it does a 302 redirect to your error URLs and then those pages respond with a 200 status code. This leads to Google indexing the error pages which is bad. It also isn't very conformant to the HTTP spec. What I wanted to do was not redirect, and overrite the original response with my custom error views.</p> <p>I tried to change <code>redirectMode="ResponseRewrite"</code>. Unfortunately, <a href="https://stackoverflow.com/questions/781861/customerrors-does-not-work-when-setting-redirectmode-responserewrite/3770265#3770265">this option does not support MVC routes</a>, only static HTML pages or ASPX. I tried to use an static HTML page at first but the response code was still 200 but, at least it didn't redirect. I then got an idea from <a href="https://softwareengineering.stackexchange.com/questions/45195/what-are-the-definitive-guidelines-for-custom-error-handling-in-asp-net-mvc-3/45197#45197">this answer</a>...</p> <p>I decided to give up on MVC for error handling. I created an <code>Error.aspx</code> and a <code>PageNotFound.aspx</code>. These pages were very simple but they had one piece of magic...</p> <pre><code>&lt;script type="text/C#" runat="server"&gt; protected override void OnLoad(EventArgs e) { base.OnLoad(e); Response.StatusCode = (int) System.Net.HttpStatusCode.InternalServerError; } &lt;/script&gt; </code></pre> <p>This block tells the page to be served with the correct status code. Of coarse, on the PageNotFound.aspx page, I used <code>HttpStatusCode.NotFound</code> instead. I changed my web.config to look like this...</p> <pre><code>&lt;customErrors mode="RemoteOnly" redirectMode="ResponseRewrite" defaultRedirect="~/Error.aspx"&gt; &lt;error statusCode="404" redirect="~/PageNotFound.aspx" /&gt; &lt;/customErrors&gt; </code></pre> <p>It all worked perfectly!</p> <p><strong>Summary:</strong></p> <ul> <li>Remove the line: <code>filters.Add(new HandleErrorAttribute());</code></li> <li>Use <code>Application_Error()</code> method to log exceptions</li> <li>Use customErrors with a ResponseRewrite, pointing at ASPX pages</li> <li>Make the ASPX pages responsible for their own response status codes</li> </ul> <p>There are a couple downsides I have noticed with this solution.</p> <ul> <li>The ASPX pages can't share any markup with Razor templates, I had to rewrite our website's standard header and footer markup for a consistent look and feel.</li> <li>The *.aspx pages can be accessed directly by hitting their URLs</li> </ul> <p>There are work-arounds for these problems but I wasn't concerned enough by them to do any extra work.</p> <p>I hope this helps everyone!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload