【问题标题】:How to determine which MasterPage an error was generated from?如何确定从哪个 MasterPage 生成错误?
【发布时间】:2014-11-24 05:56:33
【问题描述】:

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

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

【问题讨论】:

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


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2013-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2020-11-30
      • 1970-01-01
      • 2020-04-13
      • 2013-09-29
      相关资源
      最近更新 更多