如何确定哪个MasterPage生成错误?

时间:2014-09-29 18:46:35

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

我们有一个大型的ASP WebForms应用程序,我试图让错误页面看起来更好,更有帮助。我们有几个MasterPages,其中一个用于在我们通过JavaScript打开的模态窗口中显示iframe内的内容。现在,如果在其中一个模态页面中发生错误,模态iframe将被重定向到我们的~/Error.aspx页面,其中包含正常的站点设计 - 位于站点设计顶部的模式内。看起来很蠢。

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

1 个答案:

答案 0 :(得分:1)

经过大量的调试后,我似乎找到了一种方法来做我想要的VB,虽然它有点hacky ......

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