如何在 ASP.NET Page_Error 处理程序中获取没有字符串名称(例如 GetType().FullName)比较的异常类型?
有没有比检查异常字符串更好的方法?
我宁愿在页面上处理这个包罗万象的错误,但对于 SOAP 异常(Web 服务调用),我需要记录服务器上而不是客户端上发生的实际异常的详细信息。
“.Detail.InnerText”属性不在通用异常中,只能在将通用异常转换为 SOAP 异常后才能获取。
protected void Page_Error(object sender, EventArgs e)
{
Exception ex = Context.Server.GetLastError();
if (ex.GetType().FullName == "System.Web.Services.Protocols.SoapException")
{
System.Web.Services.Protocols.SoapException realException = (System.Web.Services.Protocols.SoapException)ex;
Response.Clear();
Response.Output.Write(@"<div style='color:maroon; border:solid 1px maroon;'><pre>{0}</pre></div>", realException.Detail.InnerText);
Response.Output.Write("<div style='color:maroon; border:solid 1px maroon;'><pre>{0}\n{1}</pre></div>", ex.Message, ex.StackTrace);
Context.ClearError();
Response.End();
}
}
我认为有一种方法可以在不使用字符串比较的情况下获取底层异常的类型...
提前致谢。
Is there a better way to do this than to check for Exception string?
I would rather have this catch-all error handled on the page, but for SOAP exceptions (web-service calls) I need to log the details of the actual exception that occured on the server, not the client.
The ".Detail.InnerText" property isn't in a generic exception, and can only be gotten after casting a generic exception to a SOAP exception.
protected void Page_Error(object sender, EventArgs e)
{
Exception ex = Context.Server.GetLastError();
if (ex.GetType().FullName == "System.Web.Services.Protocols.SoapException")
{
System.Web.Services.Protocols.SoapException realException = (System.Web.Services.Protocols.SoapException)ex;
Response.Clear();
Response.Output.Write(@"<div style='color:maroon; border:solid 1px maroon;'><pre>{0}</pre></div>", realException.Detail.InnerText);
Response.Output.Write("<div style='color:maroon; border:solid 1px maroon;'><pre>{0}\n{1}</pre></div>", ex.Message, ex.StackTrace);
Context.ClearError();
Response.End();
}
}
I would think there is a way to get at the underlying Exception's type without using string comparison...
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你能尝试这样的想法吗:
Can you try somethink like this:
使用
as
运算符:(请参阅Dima 的回答。)
或者比较类型对象:
假设您想要访问
SoapException
的某个成员,即as 方法避免了多重类型检查。
Either use the
as
operator:(See Dima's answer.)
Or compare type objects:
One assumes you will want to access some member of
SoapException
, theas
approach avoids multiple type checks.