ASP.NET Post Application_Error 事件
我试图找到一个在所有 Application_Error
事件处理程序之后立即触发的事件,以便我可以修改发送的响应(弄乱状态代码和“位置”标头并专门创建一个新主体)使用自定义 HttpModule。
我尝试挂钩 Application_EndRequest
(因为我读到这是唯一保证触发的处理程序),但这在请求处理中为时已晚,无法修改响应标头,我得到了HttpException
:
Server cannot append header after HTTP headers have been sent.
I'm trying to find an event that will fire immediately after all Application_Error
event handlers so that I can modify the response sent (messing with the status code and 'location' headers and creating a new body specifically) using a custom HttpModule.
I've tried hooking into Application_EndRequest
(as I've read that this is the only handler that is guaranteed to fire) but this is too late in the request processing to modify the response headers and I get a HttpException
:
Server cannot append header after HTTP headers have been sent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为 ASP.Net 在 ApplicationError 之后启动了一个新的响应,只是针对不同的错误处理页面。 尝试加入这个回应。
更新 好的:以下是具体操作方法!
我想确保我们都可以附加错误处理程序,并且可以优雅地处理页面中的事件,因此我们不能使最后一个服务器错误为空,而且我们还希望能够添加标头!
在 web.config 中,添加:
或任何您想要捕获的错误。 在 httpModules 下:
记录错误的代码位于此模块中:
我刚刚将其写入文件系统。 您可以使用内核文件事务或独占锁或其他任何东西,但因为我知道我只会从这里写入该文件,所以我选择了一个简单的私有信号量。
我在 Default.aspx.cs 中添加了这个只是为了测试:
创建了包含此文件的 ErrorHandler.aspx 文件(为简短起见,省略了一些部分):
这是页面类:
最后,我在 ErrorHandler.aspx 处获得了这些响应标头:
如果您尝试直接添加到 Response.Headers-collection,则需要以管道模式运行 IIS: http://forums.asp.net/p/1253457/2323117.aspx
我希望这有帮助! 干杯
亨里克
I think ASP.Net launches a new response after ApplicationError, just for a different error-handling page. Try tacking into this response.
Update OK: Here's how to do it!
I wanted to make sure we can both attach an error handler and that we can gracefully handle the event in a page, so we can't make the last server error null, and we also want to be able to add headers!
In web.config, add:
Or whatever error specifically you're looking to catch. And under httpModules:
The code to log the error is in this module:
I just wrote it to the filesystem. You could use kernel file transactions or an exclusive lock or whatever, but because I know I will only write to this file from here, I chose a simple private semaphore.
I added this in Default.aspx.cs just to test:
Created file ErrorHandler.aspx with this in it (some parts omitted for shortness):
And here's the page-class:
Finally, I get these response headers at the ErrorHandler.aspx:
If you try to add to Response.Headers-collection directly you need to run IIS in pipeline mode: http://forums.asp.net/p/1253457/2323117.aspx
I hope this helps! Cheers
Henrik
除非您在发送任何响应之前移动所有容易出错的代码,否则您将无法解决此问题。 如果您想这样做,则需要设置一个 HttpHandler,以便在将任何信息发送回客户端之前运行所有代码。
有多种方法可以重新架构以将信息返回给客户端。 了解应用程序在哪里崩溃并通过 try ... catch 运行它会有所帮助。
Unless you move all error prone code prior to any of the Response being sent, you will not be able to solve this issue. If you want to do this, you need to set up an HttpHandler to work through having all of the code run prior to sending any information back to the client.
There are ways to re-architect to get info back to the client. Knowing where the application blows up and running it through try ... catch will help.