简短版本:
如果我按如下方式创建System.Web.HttpException
:
var exception = new HttpException(403, "Forbidden");
我希望以下方法可以返回这些值,但它们不会:
var code = exception.GetHttpCode(); // is 0
var msg = exception.GetHtmlErrorMessage(); // is: null
编辑:实际上GetHttpCode()
在第一次调用时返回正确的数字,但在第二次调用时返回0
:
var code = exception.GetHttpCode(); // is 403
code = exception.GetHttpCode(); // is 0
长版:
我正在尝试对ASP.NET全局异常处理方法“Application_Error”进行单元测试。这是代码的摘录:
var httpException = server.GetLastError() as HttpException;
if (httpException != null)
{
response.StatusCode = httpException.GetHttpCode();
response.StatusDescription = httpException.GetHtmlErrorMessage();
// ...
单元测试使用模拟ServerUtilityBase
对象(Moq)调用此方法,该对象在调用HttpException
时返回server.GetLastError()
:
var exception = new HttpException(403, "Forbidden");
serverMock.Setup(server => server.GetLastError()).Returns(exception);
// ...
不幸的是,我必须在错误处理代码中发现httpException.GetHttpCode()
和httpException.GetHtmlErrorMessage()
方法分别返回0
或null
。
在调用这些方法时,需要做些什么来new HttpException(403, "Forbidden")
返回403
或"Forbidden"
?
不幸的是,通过继承它来创建异常模拟是不可能的,因为上述方法是密封的。
答案 0 :(得分:2)
httpException.GetHtmlErrorMessage()方法用于返回由ASP.NET运行时发送到浏览器的html,Patrick是正确的,因为您需要查看.Message属性以查看文本“forbidden” 。如果你想看看实际的html是什么样的,你需要提供一个内部异常,它是ErrorFormatter可以使用的异常类型(例如SecurityException)。
var httpException = new HttpException(403, "Forbidden", new SecurityException());
Console.WriteLine(httpException.GetHttpCode());
Console.WriteLine(httpException.Message);
Console.WriteLine(httpException.GetHtmlErrorMessage());
将输出:
403
Forbidden
<html>
<head>
<title>Security Exception</title>
<style>
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; }
</style>
</head>
<body bgcolor="white">
<span><H1>Server Error in '' Application.<hr width=100% size=1 color
=silver></H1>
<h2> <i>Security Exception</i> </h2></span>
<font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">
<b> Description: </b>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.
<br><br>
<b> Exception Details: </b>System.Security.SecurityException: Securi
ty error.<br><br>
<b>Source Error:</b> <br><br>
<table width=100% bgcolor="#ffffcc">
<tr>
<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>
</tr>
</table>
<br>
<b>Stack Trace:</b> <br><br>
<table width=100% bgcolor="#ffffcc">
<tr>
<td>
<code><pre>
[SecurityException: Security error.]
</pre></code>
</td>
</tr>
</table>
<br>
</body>
</html>