对所有 ASP.NET MVC 错误使用全局(哑)错误页面?
我已经设置并配置了 ELMAH 来记录我正在处理的 ASP.NET MVC 项目上的所有错误。 它将由一小群不需要了解太多的用户使用,因此每当出现任何类型的错误(404、InvalidOperation、Y2K...任何东西!)时,我只想向他们展示一个通用的默认错误视图,其中包含致电我们的帮助台并耐心等待的说明。
ELMAH 已启动并运行良好,我们的帮助台工作人员将使用它来诊断错误并根据需要记录/提升票证。 我的问题是如何显示我的全局错误页面。 我正在使用发布的解决方案 此处 确保 ELMAH 和 [HandleError] 能够很好地协同工作。
Web.config 设置如下:
<customErrors mode="On" />
如果我访问 http://application/Home/Index ,它有一个LINQ 错误(序列不包含任何元素),然后我会从 /Shared/Error.aspx 中看到漂亮的通用错误视图,但如果我尝试访问 http://application/Fake/Broken 这是一个 404,然后我得到通常的 ASP.NET
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Fake/Broken
两个错误都被 ELMAH 完美记录。
I've set up and configured ELMAH to log all of my errors on an ASP.NET MVC project I'm working on. It will be used by a small group of users who don't need to know too much so whenever there is any sort of error (404, InvalidOperation, Y2K... anything!) I just want to show them a generic default error view with instructions to call our helpdesk and sit tight.
ELMAH is up and running fine which our helpdesk staff will use to diagnose errors and log / elevate the tickets as necessary. My problem is in getting my global error page to show. I'm using the solution posted here to ensure that ELMAH and [HandleError] play nice together.
Web.config is set up as so:
<customErrors mode="On" />
If I access http://application/Home/Index which has a LINQ error (Sequence contains no elements) then I am shown my nice, generic error view from /Shared/Error.aspx but if I try to access http://application/Fake/Broken which is a 404 then I get the usual ASP.NET
Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Fake/Broken
Both errors get logged perfectly by ELMAH though.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您只是想显示一个通用错误页面,那么您可以指定页面本身:
这将导致所有未处理的异常、未找到页面等显示您的“error.htm”。 不过,您可以更具体地了解错误代码:
If you're just looking to show a generic error page, then you could specify the page itself:
That would cause all unhandled exceptions, page not founds, etc to show your "error.htm". You can be more specific about the error codes though:
如果您使用的是 iis 7,您可能需要将其添加到
配置文件中的部分
if you are using iis 7 you might need to add this to your
section in the config file