显示来自 Global.asax 的警报消息框(在 Application_Error 事件上)

发布于 2024-08-28 12:24:13 字数 1203 浏览 4 评论 0原文

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

我愿意接受模态弹出窗口或任何其他类型的错误消息,我只是想确保用户停留在遇到错误的页面上。

感谢您的任何想法。

这是参考此线程: 检测到潜在危险的 Request.Form 值:主动或事后处理这些错误

这是我当前使用的代码:

Sub Application_Error(ByVal 发送方作为对象,ByVal e 作为 EventArgs)
    '发生未处理错误时运行的代码
    尝试
        Dim err As Exception = Server.GetLastError
        如果 err.Message 不是什么,那么
            如果 err.Message =“客户端已断开连接。”然后
                暗淡 LogError 作为新 LogError(Server.GetLastError,会话,请求)
                LogError.LogError()
                Response.Redirect("~/TimeoutPage.aspx?id=2")
            ElseIf err.Message.Contains("危险的Request.Form值") 然后
                'Response.Redirect("~/MyInputErrorPage.aspx")
            '而不是上面的重定向,我想向用户显示一个警报框或类似的东西来向他们解释错误
            别的
                暗淡 LogError 作为新 LogError(Server.GetLastError,会话,请求)
                LogError.LogError()
                Response.Redirect("~/MyErrorPage.aspx")
            结束如果
        结束如果
    捕获 ex 作为异常
    结束尝试

Normally I just redirect to a custom error page in on the Application_Error event, but I have a specific error for which I'd like to display an alert message while the user is still on the page which triggers the error. How can I make this happen?

I'm open to a modalpopup or any other type of error message, I just want to ensure the user stays on the page where they encounter the error.

Thank for any ideas.

This is in reference to this thread: A potentially dangerous Request.Form value was detected: Dealing with these errors proactively, or after the fact

Here is the code I'm currently using:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

为你拒绝所有暧昧 2024-09-04 12:24:13

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

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

If you know where the error occurs, wrap it in a try-catch block and handle it in the code behind page load.

If you want a Javascript styled modal pop-up error, in the catch, write out a flag value / error message to a hidden variable on the page and handle it in the html document load.

梦醒灬来后我 2024-09-04 12:24:13

我无法找到一种简单的方法来做到这一点,所以我只是为这些类型的错误创建了新的错误页面,并在 Application_Error 事件中捕获该特定错误时重定向到该页面(正如我在第一篇文章中的代码所示) )。

I couldn't figure out an easy way to do this, so I just made new error page for these types of errors, and Redirect to that page when that specific error is caught in the Application_Error event (as my code in the first post indicates).

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