从 Application_OnError 获取正确的 Response.StatusCode (ASP.NET MVC)

发布于 2024-07-11 11:25:37 字数 340 浏览 7 评论 0原文

我正在尝试为 ASP.NET MVC 应用程序中的未处理异常创建一些基本日志记录,但由于我使用的是 IIS6,所以所有请求都通过 .NET 进行。 现在,虽然我同意丢失的图像是需要标记的重要事项,但它们并不是阻碍因素,因此我想将 404 的事件优先级设置为高,除非它们是图像(将获得中等优先级)。

问题是,在Application_OnError中,Response.StatusCode是200,但最终发送出去的结果是404。为此,我似乎无法观看为 404 设置适当的优先级。

有没有办法知道StatusCode将会是什么?

I'm trying to create some basic logging for unhandled exceptions in my ASP.NET MVC application, but as I'm using IIS6 all requests come through .NET. Now while I agree that missing images are an important thing to flag, they aren't show-stoppers and so I'd like to set the event priority to high for 404s unless they are images (which will get medium priority).

The problem is that in Application_OnError the Response.StatusCode is 200, but the final result sent out is 404. To this end, I don't seem to be able to watch out for 404s to set the appropriate priority.

Is there a way to tell what the StatusCode is going to be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

清泪尽 2024-07-18 11:25:37

您可以通过 Context.Server.GetLastError() 获取它:

if ((Context.Server.GetLastError() is HttpException) && ((Context.Server.GetLastError() as HttpException).GetHttpCode() == 404))
{
    // do something
}

You can get it via Context.Server.GetLastError():

if ((Context.Server.GetLastError() is HttpException) && ((Context.Server.GetLastError() as HttpException).GetHttpCode() == 404))
{
    // do something
}
余厌 2024-07-18 11:25:37

我想你必须将所有流量发送到 .net isapi 层并自己进行手动 404 检查(如果文件存在)

You would have to send all traffic through to the .net isapi layer and do a manual 404 check yourself (if file exists) I would imagine

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文