对所有 ASP.NET MVC 错误使用全局(哑)错误页面?

发布于 2024-07-25 07:20:48 字数 1239 浏览 2 评论 0原文

我已经设置并配置了 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 技术交流群。

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

发布评论

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

评论(2

酒绊 2024-08-01 07:20:48

如果您只是想显示一个通用错误页面,那么您可以指定页面本身:

<customErrors mode="On" defaultRedirect="error.htm" />

这将导致所有未处理的异常、未找到页面等显示您的“error.htm”。 不过,您可以更具体地了解错误代码:

<customErrors mode="On">
  <error statusCode="404" redirect="notfound.htm"/>
</customErrors>

If you're just looking to show a generic error page, then you could specify the page itself:

<customErrors mode="On" defaultRedirect="error.htm" />

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:

<customErrors mode="On">
  <error statusCode="404" redirect="notfound.htm"/>
</customErrors>
傾城如夢未必闌珊 2024-08-01 07:20:48

如果您使用的是 iis 7,您可能需要将其添加到

<system.webServer>

配置文件中的部分

<httpErrors errorMode="Custom" existingResponse="PassThrough" />

if you are using iis 7 you might need to add this to your

<system.webServer>

section in the config file

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