{"id":1974,"date":"2022-08-30T15:21:00","date_gmt":"2022-08-30T15:21:00","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/08\/asp-net-httpexception-gethttpcode-returns-0-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:21:00","modified_gmt":"2022-08-30T15:21:00","slug":"asp-net-httpexception-gethttpcode-returns-0-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/asp-net-httpexception-gethttpcode-returns-0-collection-of-common-programming-errors\/","title":{"rendered":"ASP.NET HttpException.GetHttpCode() returns 0-Collection of common programming errors"},"content":{"rendered":"<p><strong>Short Version:<\/strong><\/p>\n<p>If I create a <code>System.Web.HttpException<\/code> as follows:<\/p>\n<pre><code>var exception = new HttpException(403, \"Forbidden\");\n<\/code><\/pre>\n<p>I would expect the following methods to return these values, but they don&#8217;t:<\/p>\n<pre><code>var code = exception.GetHttpCode(); \/\/ is 0\nvar msg = exception.GetHtmlErrorMessage(); \/\/ is: null\n<\/code><\/pre>\n<p><em>Edit:<\/em> In fact <code>GetHttpCode()<\/code> returns the correct number when being called the first time, but returns <code>0<\/code> when being called a second time:<\/p>\n<pre><code>var code = exception.GetHttpCode(); \/\/ is 403\ncode = exception.GetHttpCode(); \/\/ is 0\n<\/code><\/pre>\n<p><strong>Long Version:<\/strong><\/p>\n<p>I am trying to unit-test the ASP.NET global exception handling method &#8220;Application_Error&#8221;. This is an extract of the code:<\/p>\n<pre><code>var httpException = server.GetLastError() as HttpException;\nif (httpException != null)\n{\n    response.StatusCode = httpException.GetHttpCode();\n    response.StatusDescription = httpException.GetHtmlErrorMessage();\n\n\/\/ ...\n<\/code><\/pre>\n<p>The unit test calls this method with a mock <code>ServerUtilityBase<\/code> object (Moq) which returns an <code>HttpException<\/code> when <code>server.GetLastError()<\/code> is called:<\/p>\n<pre><code>var exception = new HttpException(403, \"Forbidden\");\nserverMock.Setup(server =&gt; server.GetLastError()).Returns(exception);\n\/\/ ...\n<\/code><\/pre>\n<p>Unfortunately I had to find out that in the error handling code, <code>httpException.GetHttpCode()<\/code> and <code>httpException.GetHtmlErrorMessage()<\/code> methods return <code>0<\/code> or <code>null<\/code>, respectively.<\/p>\n<p>What needs to be done to make a <code>new HttpException(403, \"Forbidden\")<\/code> return <code>403<\/code> or <code>\"Forbidden\"<\/code> when calling these methods?<\/p>\n<p>Unfortunately it is not possible to create a Mock of the exception by subclassing it, because the said methods are sealed.<\/p>\n<ol>\n<li>\n<p>The httpException.GetHtmlErrorMessage() method is used to return the html which is sent to the browser by the ASP.NET Runtime, Patrick is correct in that you need to look at the .Message property to see the text &#8220;forbidden&#8221;. If you want to see what the actual html looks like, you would need to supply an inner exception which is an exception type that the ErrorFormatter can use (for example a SecurityException).<\/p>\n<pre><code>var httpException = new HttpException(403, \"Forbidden\", new SecurityException());\n\nConsole.WriteLine(httpException.GetHttpCode());\nConsole.WriteLine(httpException.Message);\nConsole.WriteLine(httpException.GetHtmlErrorMessage());\n<\/code><\/pre>\n<p>will output:<\/p>\n<pre><code>403\nForbidden\n\n    \n        Security Exception\n        \n         body {font-family:\"Verdana\";font-weight:normal;font-size: .7em;color:bl\nack;}\n         p {font-family:\"Verdana\";font-weight:normal;color:black;margin-top: -5p\nx}\n         b {font-family:\"Verdana\";font-weight:bold;color:black;margin-top: -5px}\n\n         H1 { font-family:\"Verdana\";font-weight:normal;font-size:18pt;color:red\n}\n         H2 { font-family:\"Verdana\";font-weight:normal;font-size:14pt;color:maro\non }\n         pre {font-family:\"Lucida Console\";font-size: .9em}\n         .marker {font-weight: bold; color: black;text-decoration: none;}\n         .version {color: gray;}\n         .error {margin-bottom: 10px;}\n         .expandable { text-decoration:underline; font-weight:bold; color:navy;\ncursor:hand; }\n        \n    \n\n    \n\n            <\/code><\/pre>\n<h1><code>Server Error in '' Application.<\/code><\/h1>\n<pre>\n\n            <\/pre>\n<h2><code><i>Security Exception<\/i><\/code><\/h2>\n<pre> \n\n            \n\n\n            <b> Description: <\/b>The application attempted to perform an operati\non not allowed by the security policy.  To grant this application the requi\nred permission please contact your system administrator or change the applicatio\nn's trust level in the configuration file.\n            <br \/><br \/>\n\n            <b> Exception Details: <\/b>System.Security.SecurityException: Securi\nty error.<br \/><br \/>\n\n            <b>Source Error:<\/b> <br \/><br \/>\n\n            <\/pre>\n<table width=\"100%\" bgcolor=\"#FFFFCC\">\n<tr>\n<td><code>An unhandled exception was generated during the execution of the current web req uest. Information regarding the origin and location of the exception can be iden tified using the exception stack trace below.<\/code><\/td>\n<\/tr>\n<\/table>\n<p>\n<b>Stack Trace:<\/b><\/p>\n<table width=\"100%\" bgcolor=\"#FFFFCC\">\n<tr>\n<td>\n<pre><code>\n\n[SecurityException: Security error.]\n<\/code><\/pre>\n<\/td>\n<\/tr>\n<\/table>\n<p><\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-08 01:21:07. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Short Version: If I create a System.Web.HttpException as follows: var exception = new HttpException(403, &#8220;Forbidden&#8221;); I would expect the following methods to return these values, but they don&#8217;t: var code = exception.GetHttpCode(); \/\/ is 0 var msg = exception.GetHtmlErrorMessage(); \/\/ is: null Edit: In fact GetHttpCode() returns the correct number when being called the first [&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-1974","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1974","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=1974"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1974\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1974"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1974"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1974"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}