ASP.NET HttpException.GetHttpCode() returns 0-Collection of common programming errors
Short Version:
If I create a System.Web.HttpException
as follows:
var exception = new HttpException(403, "Forbidden");
I would expect the following methods to return these values, but they don’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 time, but returns 0
when being called a second time:
var code = exception.GetHttpCode(); // is 403
code = exception.GetHttpCode(); // is 0
Long Version:
I am trying to unit-test the ASP.NET global exception handling method “Application_Error”. This is an extract of the code:
var httpException = server.GetLastError() as HttpException;
if (httpException != null)
{
response.StatusCode = httpException.GetHttpCode();
response.StatusDescription = httpException.GetHtmlErrorMessage();
// ...
The unit test calls this method with a mock ServerUtilityBase
object (Moq) which returns an HttpException
when server.GetLastError()
is called:
var exception = new HttpException(403, "Forbidden");
serverMock.Setup(server => server.GetLastError()).Returns(exception);
// ...
Unfortunately I had to find out that in the error handling code, httpException.GetHttpCode()
and httpException.GetHtmlErrorMessage()
methods return 0
or null
, respectively.
What needs to be done to make a new HttpException(403, "Forbidden")
return 403
or "Forbidden"
when calling these methods?
Unfortunately it is not possible to create a Mock of the exception by subclassing it, because the said methods are sealed.
-
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 “forbidden”. 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).
var httpException = new HttpException(403, "Forbidden", new SecurityException()); Console.WriteLine(httpException.GetHttpCode()); Console.WriteLine(httpException.Message); Console.WriteLine(httpException.GetHtmlErrorMessage());
will output:
403 Forbidden Security Exception body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:bl ack;} p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5p x} b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px} H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red } H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maro on } pre {font-family:"Lucida Console";font-size: .9em} .marker {font-weight: bold; color: black;text-decoration: none;} .version {color: gray;} .error {margin-bottom: 10px;} .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
Server Error in '' Application.
Security Exception
Description: The application attempted to perform an operati on not allowed by the security policy. To grant this application the requi red permission please contact your system administrator or change the applicatio n's trust level in the configuration file.
Exception Details: System.Security.SecurityException: Securi ty error.
Source Error:
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.
Stack Trace:
[SecurityException: Security error.]