如何在 ASP.NET Page_Error 处理程序中获取没有字符串名称(例如 GetType().FullName)比较的异常类型?

发布于 2024-10-07 10:10:35 字数 1030 浏览 3 评论 0原文

有没有比检查异常字符串更好的方法?

我宁愿在页面上处理这个包罗万象的错误,但对于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我的奇迹 2024-10-14 10:10:35

你能尝试这样的想法吗:

var ex = Context.Server.GetLastError();

var soapEx = ex as SoapException;
if(soapEx != null)
{
    //Handle SoapException
}

Can you try somethink like this:

var ex = Context.Server.GetLastError();

var soapEx = ex as SoapException;
if(soapEx != null)
{
    //Handle SoapException
}
情痴 2024-10-14 10:10:35

使用 as 运算符:(

var ex = Context.Server.GetLastError();

var soapEx = ex as SoapException;
if(soapEx != null)
{
    //Handle SoapException
}

请参阅Dima 的回答。)

或者比较类型对象:

var ex = Context.Server.GetLastError();
if (ex.GetType() == typeof(SoapException) {
  ..
}

假设您想要访问 SoapException 的某个成员,即 as 方法避免了多重类型检查。

Either use the as operator:

var ex = Context.Server.GetLastError();

var soapEx = ex as SoapException;
if(soapEx != null)
{
    //Handle SoapException
}

(See Dima's answer.)

Or compare type objects:

var ex = Context.Server.GetLastError();
if (ex.GetType() == typeof(SoapException) {
  ..
}

One assumes you will want to access some member of SoapException, the as approach avoids multiple type checks.

递刀给你 2024-10-14 10:10:35
if (ex is System.Web.Services.Protocols.SoapException)
{
    var soapEx = (System.Web.Services.Protocols.SoapException)ex;
    // do your thing
}
if (ex is System.Web.Services.Protocols.SoapException)
{
    var soapEx = (System.Web.Services.Protocols.SoapException)ex;
    // do your thing
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文