【问题标题】:Show alert message box from the Global.asax (on Application_Error event)显示来自 Global.asax 的警报消息框(在 Application_Error 事件上)
【发布时间】:2011-02-07 00:27:54
【问题描述】:

通常我只是重定向到 Application_Error 事件中的自定义错误页面,但我有一个特定的错误,我想在用户仍在触发错误的页面上时显示一条警报消息。我怎样才能做到这一点?

我对 modalpopup 或任何其他类型的错误消息持开放态度,我只是想确保用户停留在他们遇到错误的页面上。

感谢您的任何想法。

这是参考这个帖子:A potentially dangerous Request.Form value was detected: Dealing with these errors proactively, or after the fact

这是我目前使用的代码:

Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    'Code that runs when an unhandled error occurs
    Try
        Dim err As Exception = Server.GetLastError
        If err.Message IsNot Nothing Then
            If err.Message = "The client disconnected." Then
                Dim LogError As New LogError(Server.GetLastError, Session, Request)
                LogError.LogError()
                Response.Redirect("~/TimeoutPage.aspx?id=2")
            ElseIf err.Message.Contains("dangerous Request.Form value") Then
                'Response.Redirect("~/MyInputErrorPage.aspx")
            'Instead the above redirect, I'd like to show the user an alertbox or something similar to explain the error to them
            Else
                Dim LogError As New LogError(Server.GetLastError, Session, Request)
                LogError.LogError()
                Response.Redirect("~/MyErrorPage.aspx")
            End If
        End If
    Catch ex As Exception
    End Try

【问题讨论】:

  • 您能否定义需要此功能的特定情况?一旦用户点击某些东西并触发请求,他们就不再真正“在页面上”了。为什么不直接在页面本身添加错误处理?
  • 有大量页面可能会触发此错误,我不想更新所有页面..这就是我尝试在 Global.asax 中处理此问题的原因请查看修改后的问题...这是关于在用户输入潜在危险字符(例如 字符)时捕获错误...我不想在每个页面/输入控件上对这些字符进行验证。

标签: .net asp.net vb.net error-handling global-asax


【解决方案1】:

如果您知道错误发生的位置,请将其包装在 try-catch 块中,并在页面加载后的代码中进行处理。

如果你想要一个 Javascript 样式的模态弹出错误,在 catch 中,将标志值/错误消息写入页面上的隐藏变量并在 html 文档加载中处理它。

【讨论】:

  • 嗯,那么这是另一个想法 - 在 Global.asax 中,在“on request”处理程序中捕获请求的 URL。当您捕获错误时将请求重定向回页面,但注入代码会导致弹出窗口。
  • 您是否正在使用您的页面继承自的基本页面?如果是这样,把处理这个的代码放在那里
  • ^ 实际上我确实使用了母版页,我以后可能会尝试。谢谢
【解决方案2】:

我想不出一个简单的方法来做到这一点,所以我只是为这些类型的错误创建了新的错误页面,并在 Application_Error 事件中捕获到该特定错误时重定向到该页面(就像我在第一篇文章表示)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-20
    • 1970-01-01
    相关资源
    最近更新 更多