【问题标题】:exception catching in Global.ascx app_errorGlobal.ascx app_error 中的异常捕获
【发布时间】:2011-08-17 20:30:51
【问题描述】:

我在 asp.net 和 C# 中有 Web 应用程序

如果异常发生在此应用程序中的任何位置,我会尝试处理它们。

假设行为应该是如果发生这样的异常

//generate your fictional exception
            int x = 1;
            int y = 0;
            int z = x / y;

它应该在 global.ascx 文件的 app_error 中捕获它并将其重定向到 Default.aspx 页面。我得到了日志记录部分,但重定向不起作用,因为我仍然得到 ​​p>

“/”应用程序中的服务器错误。 页。或者可能是它正在重定向并在中间被杀死..

这就是 global.ascx 中的内容

 protected void Application_Error(object sender, EventArgs e)
    {
        logger.Fatal(this.Server.GetLastError().GetBaseException());
        logger.Info("FatalLogger Passed");
        //get reference to the source of the exception chain
        Exception ex = Server.GetLastError().GetBaseException();
        Response.Redirect("~/Default.aspx?error=MessageHere");
    }

这在 web.config 的代码中

<authentication mode="Forms">
        <forms loginUrl="Login.aspx" defaultUrl="~/Default.aspx" name="GUI" slidingExpiration="true" timeout="30" path="/">
        </forms>
    </authentication>

任何想法.. 生病;很乐意提供更多信息。

谢谢

好的,我想要这种方法是有原因的,因为每当出现错误时,用户都会退出,我不希望这种情况发生,而是转到默认页面

【问题讨论】:

    标签: c# asp.net exception error-handling response


    【解决方案1】:

    您是否尝试过在 Application_Error 中重定向之前调用 Server.ClearError()?我玩这个已经有一段时间了,但我相信如果你不调用 ClearError 那么框架仍然认为错误未处理。

    【讨论】:

      【解决方案2】:

      配置custom error pages

      顺便说一句,我推荐ELMAH for the logging part...

      【讨论】:

      • 我使用 log for net 进行日志记录.. 这有什么不同
      • 不仅仅是记录。当出现问题时,它会记录您想知道的任何内容(服务器、请求变量)、异常详细信息、堆栈跟踪,并提供一个很好的错误概览页面,所有这些几乎都是零工作,几乎不需要配置。
      【解决方案3】:

      尝试使用Server.Transfer(page)

      还要小心通过查询字符串传递错误消息,因为它可能会使您面临 XSS 问题。传递错误代码,然后根据代码显示消息(使用switch 语句)

      【讨论】:

        猜你喜欢
        • 2014-11-24
        • 1970-01-01
        • 1970-01-01
        • 2022-12-02
        • 1970-01-01
        • 2010-09-28
        • 1970-01-01
        相关资源
        最近更新 更多