{"id":6317,"date":"2014-04-15T19:45:54","date_gmt":"2014-04-15T19:45:54","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/04\/15\/custom-error-pages-on-asp-net-mvc3-collection-of-common-programming-errors\/"},"modified":"2014-04-15T19:45:54","modified_gmt":"2014-04-15T19:45:54","slug":"custom-error-pages-on-asp-net-mvc3-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/04\/15\/custom-error-pages-on-asp-net-mvc3-collection-of-common-programming-errors\/","title":{"rendered":"Custom error pages on asp.net MVC3-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;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 &#8220;Error&#8221; Controller where his main action is &#8220;Index&#8221; (generic error page) and this controller will have a couple more actions for the errors that may appear to the user like &#8220;Handle500&#8221; or &#8220;HandleActionNotFound&#8221;.<\/p>\n<p>So every error that may happen on the website may be handled by this &#8220;Error&#8221; Controller (examples: &#8220;Controller&#8221; or &#8220;Action&#8221; not found, 500, 404, dbException, etc).<\/p>\n<p>I am using Sitemap file to define website paths (and not route).<\/p>\n<p><strong>This question was already answered, this is a reply to Gweebz<\/strong><\/p>\n<p>My final applicaiton_error method is the following:<\/p>\n<pre><code>protected void Application_Error() {\n\/\/while my project is running in debug mode\nif (HttpContext.Current.IsDebuggingEnabled &amp;&amp; WebConfigurationManager.AppSettings[\"EnableCustomErrorPage\"].Equals(\"false\"))\n{\n    Log.Logger.Error(\"unhandled exception: \", Server.GetLastError());\n}\nelse\n{\n    try\n    {\n        var exception = Server.GetLastError();\n\n        Log.Logger.Error(\"unhandled exception: \", exception);\n\n        Response.Clear();\n        Server.ClearError();\n        var routeData = new RouteData();\n        routeData.Values[\"controller\"] = \"Errors\";\n        routeData.Values[\"action\"] = \"General\";\n        routeData.Values[\"exception\"] = exception;\n\n        IController errorsController = new ErrorsController();\n        var rc = new RequestContext(new HttpContextWrapper(Context), routeData);\n        errorsController.Execute(rc);\n    }\n    catch (Exception e)\n    {\n        \/\/if Error controller failed for same reason, we will display static HTML error page\n        Log.Logger.Fatal(\"failed to display error page, fallback to HTML error: \", e);\n        Response.TransmitFile(\"~\/error.html\");\n    }\n}\n}\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;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 &#8220;Error&#8221; Controller where his main action is &#8220;Index&#8221; (generic error page) and this controller will have a couple more actions for the errors that [&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-6317","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6317","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=6317"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/6317\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=6317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=6317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=6317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}