{"id":205,"date":"2022-08-30T14:57:28","date_gmt":"2022-08-30T14:57:28","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/08\/11\/how-can-i-properly-handle-404-in-asp-net-mvc-collection-of-common-programming-errors\/"},"modified":"2022-08-30T14:57:28","modified_gmt":"2022-08-30T14:57:28","slug":"how-can-i-properly-handle-404-in-asp-net-mvc-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-can-i-properly-handle-404-in-asp-net-mvc-collection-of-common-programming-errors\/","title":{"rendered":"How can I properly handle 404 in ASP.NET MVC?-Collection of common programming errors"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/i.stack.imgur.com\/Zl4jh.png\" \/><\/p>\n<p>For the lazy guys out there:<\/p>\n<pre><code>Install-Package MagicalUnicornMvcErrorToolkit -Version 1.0\n<\/code><\/pre>\n<p>Then remove this line from <code>global.asax<\/code><\/p>\n<pre><code>GlobalFilters.Filters.Add(new HandleErrorAttribute());\n<\/code><\/pre>\n<p><em>And this is only for IIS7+ and IIS Express.<\/em><\/p>\n<p>If you&#8217;re using Cassini .. well .. um .. er.. awkward &#8230;<\/p>\n<h3>Long, explained answer<\/h3>\n<p>I know this has been answered. But the answer is REALLY SIMPLE (cheers to David Fowler and Damian Edwards for really answering this).<\/p>\n<p>There is <em>no need to do anything custom<\/em>.<\/p>\n<p>For <code>ASP.NET MVC3<\/code>, all the bits and pieces are there.<\/p>\n<h3>Step 1 -&gt; Update your web.config in TWO spots.<\/h3>\n<pre><code>\n    \n      \n    \n<\/code><\/pre>\n<p>and<\/p>\n<pre><code>\n    \n      \n      \n      \n      \n        \n\n...\n\n...\n\n<\/code><\/pre>\n<p>Now take careful note of the ROUTES I&#8217;ve decided to use. You can use anything, but my routes are<\/p>\n<ul>\n<li><code>\/NotFound<\/code> <code>axd's<\/code> and <code>favicons<\/code> (ooo! bonus ignore route, for you!) Then (and the order is IMPERATIVE HERE), I have my two explicit error handling routes .. followed by any other routes. In this case, the default one. Of course, I have more, but that&#8217;s special to my web site. <em>Just make sure the error routes are at the top of the list. Order is imperative<\/em>.\n<p>Finally, while we are inside our <code>global.asax<\/code> file, we do NOT globally register the HandleError attribute. No, no, no sir. Nadda. Nope. Nien. Negative. Noooooooooo&#8230;<\/p>\n<p>Remove this line from <code>global.asax<\/code><\/p>\n<pre><code>GlobalFilters.Filters.Add(new HandleErrorAttribute());\n<\/code><\/pre>\n<h3>Step 3 &#8211; Create the controller with the action methods<\/h3>\n<p>Now .. we add a controller with two action methods &#8230;<\/p>\n<pre><code>public class ErrorController : Controller\n{\n    public ActionResult NotFound()\n    {\n        Response.StatusCode = (int)HttpStatusCode.NotFound;\n        return View();\n    }\n\n    public ActionResult ServerError()\n    {\n        Response.StatusCode = (int)HttpStatusCode.InternalServerError;\n\n        \/\/ Todo: Pass the exception into the view model, which you can make.\n        \/\/       That's an exercise, dear reader, for -you-.\n        \/\/       In case u want to pass it to the view, if you're admin, etc.\n        \/\/ if (User.IsAdmin) \/\/<\/code><\/pre>\n<\/li>\n<\/ul>\n<p id=\"rop\"><small>Originally posted 2013-08-11 02:47:20. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>For the lazy guys out there: Install-Package MagicalUnicornMvcErrorToolkit -Version 1.0 Then remove this line from global.asax GlobalFilters.Filters.Add(new HandleErrorAttribute()); And this is only for IIS7+ and IIS Express. If you&#8217;re using Cassini .. well .. um .. er.. awkward &#8230; Long, explained answer I know this has been answered. But the answer is REALLY SIMPLE (cheers [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-205","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=205"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/205\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}