gpt4 book ai didi

asp.net - ASP.NET HttpException.GetHttpCode()返回0

转载 作者:行者123 更新时间:2023-12-03 08:15:51 26 4
gpt4 key购买 nike

缩写:

如果我按以下方式创建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()方法分别返回 0null

调用这些方法时,需要做些什么才能使 new HttpException(403, "Forbidden")返回 403"Forbidden"

不幸的是,不可能通过子类化来创建异常的Mock,因为所述方法是密封的。

最佳答案

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. &nbsp;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>

关于asp.net - ASP.NET HttpException.GetHttpCode()返回0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6612936/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com