.net - 从Global.asax显示警报消息框(在Application_Error事件上)

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

通常,我只是重定向到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

最佳答案

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

如果要使用Javascript样式的模式 pop 错误,请在捕获中将标志值/错误消息写到页面上的隐藏变量中,并在html文档加载中处理它。

关于.net - 从Global.asax显示警报消息框(在Application_Error事件上),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2622366/

相关文章:

javascript - 如何在asp.net中使用 'onKeyUp'属性?

c# - 在某些 .NET Parallel.ForEach() 代码中执行一些异步/等待是否可以?

c# - 如何将文件打印到特定打印机

javascript - 如何初始化一个包含方法的类

c# - 如何使用 CSS 或 CSSClass 或 C# 更改多个不同样式的 asp.net 文本框或标签或标签?

asp.net - 使面板在 asp.net 中可见或不可见

C#:限制方法参数中的类型(不是泛型参数)

c# - C# .NET 中任意长的 Parallel.For 循环

VB.NET 如果我想锁定多个东西,我是否需要多个 SyncLocks?

c# - XML 文档中的多语言示例