简体   繁体   English

将异常传递到 ASP.net/C# 中的错误屏幕

[英]Passing Exceptions to an error screen in ASP.net/C#

Coming from a desktop background I'm not sure exactly how to pass the exceptions I have caught to an Error page in order to avoid the standard exception screen being seen by my users.来自桌面背景,我不确定如何将捕获的异常传递到错误页面,以避免我的用户看到标准异常屏幕。

My general question is how do I pass the exception from page X to my Error page in ASP.net?我的一般问题是如何将异常从页面 X 传递到 ASP.net 中的错误页面?

I suggest using the customErrors section in the web.config:我建议使用 web.config 中的 customErrors 部分:

   <customErrors mode="RemoteOnly" defaultRedirect="/error.html">
      <error statusCode="403" redirect="/accessdenied.html" />
      <error statusCode="404" redirect="/pagenotfound.html" />
   </customErrors>

And then using ELMAH to email and/or log the error.然后使用ELMAH发送电子邮件和/或记录错误。

The pattern I use is to log the error in a try/catch block (using log4net), then do a response.redirect to a simple error page.我使用的模式是在 try/catch 块中记录错误(使用 log4net),然后执行 response.redirect 到一个简单的错误页面。 This assumes you don't need to show any error details.这假设您不需要显示任何错误详细信息。

If you need the exception details on a separate page, you might want to look at Server.GetLastError.如果您需要单独页面上的异常详细信息,您可能需要查看 Server.GetLastError。 I use that in global.asax (in the Application_Error event) to log unhandled exceptions and redirect to an error page.我在 global.asax(在 Application_Error 事件中)使用它来记录未处理的异常并重定向到错误页面。

We've had good luck capturing exceptions in the Global.asax Application_Error event, storing them in session, and redirecting to our error page.我们很幸运在 Global.asax Application_Error 事件中捕获异常,将它们存储在会话中,并重定向到我们的错误页面。 Alternately you could encode the error message and pass it to the error page in the querystring.或者,您可以对错误消息进行编码并将其传递到查询字符串中的错误页面。

您还可以从

Server.GetLastError();

使用asp.net中的自定义错误页面,你可以在web.config的customError部分找到它

We capture the exception in the Global.asax file, store it in Session, the user is then redirected to the Error Page where we grab the exception for our Session variable and display the Message information to the user.我们在 Global.asax 文件中捕获异常,将其存储在 Session 中,然后将用户重定向到错误页面,在那里我们获取 Session 变量的异常并向用户显示消息信息。

    protected void Application_Error(object sender, EventArgs e)
    {
        Exception ex = Server.GetLastError();
        this.Session[CacheProvider.ToCacheKey(CacheKeys.LastError)] = ex;
    }

We do log the error message prior to displaying it the user.我们会在向用户显示之前记录错误消息。

I think you can use the global.asax -- Application_Exception handler to catch the exception and then store it for displaying in an error page.我认为您可以使用global.asax -- Application_Exception处理程序来捕获异常,然后将其存储以显示在错误页面中。

But actually, your error page shouldn't contains code that might cause just another error.但实际上,您的错误页面不应包含可能仅导致另一个错误的代码。 It should be simple "Oops! something went wrong" page.它应该是简单的“哎呀!出了点问题”页面。

If you want details on the error, use Windows' events viewer or ELMAH or employ some logging mechanism.如果您想了解错误的详细信息,请使用 Windows 的事件查看器或ELMAH或使用某些日志记录机制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM