如何让 404 与 Elmah 以及在 asp.net mvc 中为 Elmah 编写的 HandleErrorAttribute 一起使用?
我在我的一个 ASP.NET MVC 项目中实现了 Elmah。此外,我还执行了以下操作:
- 我在 post
- 我向所有控制器添加了该属性,
- 我已设置 Elmah 来过滤掉 404 错误,因为我不想记录那些
- 我添加了 Error.aspx 视图的错误在我希望用于所有错误的共享文件夹中。
- 我的 Web.Config 中的 customErrors 已打开,并且我没有在其中指定任何错误页面,因为我不应该这样做。
在我的一个控制器中,我有以下代码:
if (model == null)
throw new HttpException((int)HttpStatusCode.NotFound, "Project not found");
如果在数据库中找不到记录,我只想显示 404。由于某种原因,我的 Error.aspx 页面不会显示 404 错误。相反,它显示通用的 asp.net“找不到资源”页面。我尝试删除 Elmah 中的 404 过滤器设置,但这并没有改变任何东西。
有趣的是,如果我将错误从 NotFound 更改为 InternalServerError,Error.aspx 页面就会正常显示。
有谁知道这是为什么吗?
I have implemented Elmah in one of my ASP.NET MVC projects. In addition I did the following:
- I implemented the ElmahHandleErrorAttribute written by Atif Aziz in this post
- I added the attribute to all my controllers
- I have setup Elmah to filter out 404 errors since I do not want to log those
- I added an Error.aspx view in the Shared folder that I wish to use for all my errors.
- My customErrors in the Web.Config are turned on and I have not specifed any error pages in it as I shouldn't have to.
In one of my controllers I have the following code:
if (model == null)
throw new HttpException((int)HttpStatusCode.NotFound, "Project not found");
I simply want to display a 404 if a record can not be found in the database. For some reason my Error.aspx page will not display for a 404 error. Instead it displays the generic asp.net "the resource can not be found" page. I have tried to remove the 404 filter setup in Elmah but that does not change anything.
The interesting part is if I change the error from NotFound to say InternalServerError the Error.aspx page shows up just fine.
Does anyone have any pointers as to why this is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
除了创建示例应用程序之外,我没有使用 Elmah - 但也许是因为 404 不是应用程序错误?如果我的理解是正确的,404 错误会被推送回 IIS,然后 IIS 会显示您所看到的错误消息。另外,我相信 web.coin
如果我理解正确,您可能需要修改您的方法并使用此代替或将 Response.StatusCode 设置为 404(查看链接问题中 Jeff 的答案)
I have not used Elmah beyond creating a sample app - but maybe because a 404 is not application error? If my understanding is correct, a 404 error gets pushed back to IIS which in turn displays the error message you are seeing. Also, I believe that the web.coin
If i understand correctly, you may want to amend your approach and build use this instead or set the
Response.StatusCode
to 404 (Check out Jeff's answer in the linked question)