Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found an <a href="http://kitsula.com/Article/MVC-Custom-Error-Pages" rel="nofollow noreferrer">article</a> 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.</p> <p>The solution is to use the <a href="http://www.iis.net/ConfigReference/system.webServer/httpErrors" rel="nofollow noreferrer"><code>&lt;httpErrors&gt;</code></a> element of the <code>&lt;system.webServer&gt;</code> section.</p> <p>I configured my <em>Web.config</em> like so...</p> <pre><code>&lt;httpErrors errorMode="DetailedLocalOnly" existingResponse="Replace"&gt; &lt;remove statusCode="404" subStatusCode="-1" /&gt; &lt;remove statusCode="500" subStatusCode="-1" /&gt; &lt;error statusCode="404" path="/Error/NotFound" responseMode="ExecuteURL" /&gt; &lt;error statusCode="500" path="/Error" responseMode="ExecuteURL" /&gt; &lt;/httpErrors&gt; </code></pre> <p>I also configured <code>customErrors</code> to have <code>mode="Off"</code> (as suggested by the article).</p> <p>That makes the responses overriden by an ErrorController's actions. Here is that controller:</p> <pre><code>public class ErrorController : Controller { public ActionResult Index() { return View(); } public ActionResult NotFound() { return View(); } } </code></pre> <p>The views are very straight forward, I just used standard Razor syntax to create the pages.</p> <p>That alone should be enough for you to use custom error pages with MVC.</p> <p>I also needed logging of Exceptions so I stole the <a href="https://stackoverflow.com/a/7551256/346561">Mark's solution</a> of using a custom ExceptionFilter...</p> <pre><code>public class ExceptionPublisherExceptionFilter : IExceptionFilter { public void OnException(ExceptionContext exceptionContext) { var exception = exceptionContext.Exception; var request = exceptionContext.HttpContext.Request; // log stuff } } </code></pre> <p>The last thing you need to so is register the Exception Filter in your <em>Global.asax.cs</em> file:</p> <pre><code>public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new ExceptionPublisherExceptionFilter()); filters.Add(new HandleErrorAttribute()); } </code></pre> <p>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!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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