简体   繁体   English

如何在ASP.NET请求生命周期中处理异常

[英]How to handle exceptions during an ASP.NET request lifecycle

This question is kind of related to Handle URI hacking gracefully in ASP.NET in that it's too about how to best handle exceptions that occur during an ASP.NET request lifecycle. 这个问题与在ASP.NET优雅地处理URI黑客有关,它与如何最好地处理ASP.NET请求生命周期中发生的异常有关。 I've found a way to handle most exceptions gracefully, but then I've found that some exceptions occur so late in the request that there's no way to do stuff like Server.Transfer to compartementalize the whole error presentation logic into its own page. 我已经找到了一种可以正常处理大多数异常的方法,但是后来我发现某些异常在请求中出现得太晚了,以至于无法像Server.Transfer这样将整个错误表示逻辑分段到自己的页面中。

So, I have to handle the exception inside the Application_Error event instead, and do Response.Write s and whatnot. 因此,我必须改为处理Application_Error事件内的异常,并执行Response.Write和诸如此类。 It's ugly. 它很丑。 I understand that in some circumstances the response stream could have already been flushed, so transferring the request isn't really an option. 我了解在某些情况下响应流可能已经被刷新,因此传输请求实际上不是一种选择。 What I want to ask is if there's anyone who's found an elegant solution to this problem? 我想问的是,是否有人找到解决这个问题的好方法?

Also, I find it difficult to know when I can handle the exception gracefully by transferring the request to another page and not. 另外,我发现很难知道何时可以通过将请求转移到另一个页面来优雅地处理异常。 What is the best way to find out where in the request lifecycle we're at when an exception occurs? 找出异常发生时我们在请求生命周期中的哪个位置的最佳方法是什么? If it occurs during the loading and rendering of a page, Page_Error will be able to handle it and I've not had a problem doing Server.Transfer there yet. 如果在加载和呈现页面期间发生这种情况, Page_Error将能够处理它,并且在那里进行Server.Transfer我还没有问题。 But if the exception occurs either too early or too late for Page_Error to catch it and it bubbles to Application_Error , what do I do to know whether it's early or late? 但是,如果异常发生得太早或太迟而导致Page_Error不能捕获它,并且它冒泡到Application_Error ,我该怎么办才能知道它是早还是晚?

If it's late in the lifecycle, I'll probably have to do Response.Write directly from Application_Error , but if it's early I can do Server.Transfer . 如果是生命周期的后期,我可能必须直接从Application_ErrorResponse.Write ,但是如果是早期,我可以做Server.Transfer The problem is that trying to do Server.Transfer will itself cause an exception if it's too in the request to do it. 问题是,尝试执行Server.Transfer本身会导致异常,如果请求中也是如此。

So, is there a global enumeration or something similar that will indicate whether it's too late to do creative stuff with the response or not? 那么,是否存在一个全局枚举或类似的东西来表明是否可以用响应来进行创意工作为时已晚?

I have used this approach to catch all errors generated, either in web controls or pages. 我使用这种方法来捕获在Web控件或页面中生成的所有错误。 All it requires you to do is to inherit from a base class (one for pages and one for usercontrols) each page or usercontrol can implement its own HandleException method and do whatever it needs to do. 它需要做的就是从基类继承(一个用于页面,一个用于用户控件),每个页面或用户控件都可以实现自己的HandleException方法并执行所需的操作。 Full code here: 完整代码在这里:

Transparent generic exception handling for asp.net / MOSS2007 (with code) asp.net/MOSS2007的透明通用异常处理(带代码)

I think my advice for this would be to use ASP.NET Health Monitoring with WMI Events provider for the errors: 我认为对此的建议是将ASP.NET Health Monitoring与WMI Events提供程序结合使用以解决错误:

Here is a how to. 这是一个方法。

http://msdn.microsoft.com/en-us/library/ms178713.aspx http://msdn.microsoft.com/en-us/library/ms178713.aspx

Hope this helps: 希望这可以帮助:

Andrew 安德鲁

I suggest that you use asp.net configuration to have a general error page for the unhandled exceptions. 我建议您使用asp.net配置为未处理的异常提供常规错误页面。 From the sample web.config 从示例web.config

    <!--
        The <customErrors> section enables configuration 
        of what to do if/when an unhandled error occurs 
        during the execution of a request. Specifically, 
        it enables developers to configure html error pages 
        to be displayed in place of a error stack trace.

    <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
        <error statusCode="403" redirect="NoAccess.htm" />
        <error statusCode="404" redirect="FileNotFound.htm" />
    </customErrors>
    -->

On the overall handler, just log the exception, and let asp.net do the redirect. 在整体处理程序上,只需记录异常,然后让asp.net进行重定向。

If you still want to go on with your customer approach, I suggest you look at the available asp.net source and check how it is doing that. 如果您仍然想继续使用客户方法,建议您查看可用的asp.net源,并检查其工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM