asp.net - 如何确定从哪个MasterPage生成了错误?

标签 asp.net error-handling webforms master-pages

我们有一个大型的ASP WebForms应用程序,我正在尝试使错误页面看起来更好并且更有帮助。我们有几个MasterPage,其中一个用于在通过JavaScript打开的模式窗口中的iframe中显示内容。现在,如果在这些模式页面之一中发生错误,则模式iframe将重定向到我们的~/Error.aspx页面,该页面包含正常的网站设计-在网站设计顶部的模式内。看起来很笨。

我想做的是确定生成错误的页面的母版,然后更改“错误”页面的母版,以便它不会从iframe内部重复设计。这可能吗?

最佳答案

经过大量调试后,我似乎找到了一种在VB中完成我想做的事情的方法,尽管它有点笨拙...

Public Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    'Attempt to determine if this error came from a page displayed within a modal
    Dim page = TryCast(HttpContext.Current.CurrentHandler, _BasePage)
    If page IsNot Nothing Then
        Dim masterPageFile As String = page.MasterPageFile
        'The best we can to here is to find the masterpage and see if it
        'contains the string "modal" since that's normally how we name these things
        If Not String.IsNullOrWhiteSpace(masterPageFile) AndAlso masterPageFile.Contains("modal", StringComparison.OrdinalIgnoreCase) Then
            '==============
            'We did it!!!
            '==============
         End If
    Else
        'Either `HttpContext.Current.CurrentHandler` or `Page` is `Nothing`, so this might be a 404 or other error.
        'Let's see if there are other ways to determine if we are in a modal.

        Dim pathToAttemptedFile = HttpContext.Current.Request.Path
        Dim referrerUrl = HttpContext.Current.Request.UrlReferrer.AbsolutePath

        If (Not String.IsNullOrWhiteSpace(pathToAttemptedFile) AndAlso pathToAttemptedFile.Contains(tokenInUrlPath, StringComparison.OrdinalIgnoreCase)) OrElse
           (Not String.IsNullOrWhiteSpace(referrerUrl) AndAlso referrerUrl.Contains(tokenInUrlPath, StringComparison.OrdinalIgnoreCase)) Then
            '==============
            'We did it!!!
            '==============
        End If
    End If
End Sub

关于asp.net - 如何确定从哪个MasterPage生成了错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26106703/

相关文章:

python - 如果列表索引超出范围python错误,则分配变量

asp.net - MVC 和 WebForms 之间共享大师 - 处理 <form>

c# - 了解 Autofac 中间件与 ASP.NET Webforms、OWIN 和 ASP.NET Identity 的使用

asp.net - 使用电子邮件而不是用户名恢复密码

jquery - 从 ASP.NET 中的母版页或默认页链接到 jquery 脚本文件

asp.net - ASP 菜单未显示

error-handling - 将所有内部服务器错误插入 Windows 事件日志

PHP 错误处理 - 重定向

c# - 客户端自定义数据注释验证

javascript - 表单和 jQuery