【问题标题】:How to override web.config customErrors settings at runtime?如何在运行时覆盖 web.config customErrors 设置?
【发布时间】:2011-06-20 06:03:42
【问题描述】:

我的 ASP.NET C# 应用程序的 web.config 文件中有以下代码:

<!-- Turn on Custom Errors -->
<!-- Switch the mode to RemoteOnly for Retail/Production -->
<!-- Switch the mode to On for to see error pages in the IDE during development -->
<customErrors mode="On" defaultRedirect="ErrorPage.aspx">
   <error statusCode="403" redirect="ErrorPage403.aspx"/>
   <error statusCode="404" redirect="ErrorPage404.aspx"/>
</customErrors>

当我在本地访问我的网站 (http://ipredikt.com/ErrorPage.aspx) 时,这非常适用于错误,但我也有一个 Facebook 版本的应用程序,其中所有页面都使用不同的 MasterPage,因此使用不同的 URL (@ 987654322@).

当我在 Facebook 应用程序模式下运行时,是否可以在运行时修改 customError 重定向值,就好像我在 web.config 中有以下设置一样:

<customErrors mode="On" defaultRedirect="ErrorPageFB.aspx">
   <error statusCode="403" redirect="ErrorPage403FB.apx"/>
   <error statusCode="404" redirect="ErrorPage404FB.apx"/>
</customErrors>

我认为我不能在应用程序范围内设置它,因为它是我的应用程序中的各个页面,它们知道它们是否在 Facebook 模式下运行。

【问题讨论】:

    标签: asp.net web-config runtime custom-errors


    【解决方案1】:

    您好,我认为您可以做的是根据引荐来源网址在您的自定义错误页面中进行另一个重定向 - Request.UrlReferrer 有时引荐来源网址为空,因此请确保您处理该问题

    【讨论】:

    • 请参阅下面有关 UrlReferrer 为空的评论。
    【解决方案2】:

    所以这是一个蛮力解决方案。我在页面上使用它来处理非 Facebook 模式 404 错误:

      protected override void OnInit(System.EventArgs e)
      {         
         // If the user tries, for example, to navigate to" /fb/foo/bar
         // then the Request.Url.Query will be as follows after the 404 error: ?aspxerrorpath=/fb/foo/bar
         string queryString = Request.RequestContext.HttpContext.Request.Url.Query;
    
         string[] str = queryString.Split('=');
    
         if (str.Length > 0)
         {
            string[] str2 = str[1].Split('/');
    
            if (str2.Length > 1)
            {
               string test = str2[1].ToLowerInvariant();
    
               if (test == "fb")
               {
                  string pathAndQuery = Request.RequestContext.HttpContext.Request.Url.PathAndQuery;
                  string absolutePath = Request.RequestContext.HttpContext.Request.Url.AbsolutePath;
    
                  string mungedVirtualPath = pathAndQuery.Replace(absolutePath, "/ErrorPage404FB.aspx");
    
                  Response.Redirect(mungedVirtualPath);
               }
            }
         }
    
         base.OnInit(e);
      }
    

    不太理想,但确实有效。

    【讨论】:

    • 将 Server.Transfer() 更改为 Response.Redirect() 因为我们希望修改后的 URL 可见。 Server.Transfer 采用新 URL,但显示原始 URL。
    【解决方案3】:

    “Facebook 模式”似乎可以在 Session 中跟踪,可以在 ErrorPage.aspx 中访问以触发到 ErrorPageFB.aspx 的传输。

    更新 - 您可以使用 Request.QueryString 清理蛮力解决方案:

    protected override void OnInit(System.EventArgs e)
    {         
        // If the user tries, for example, to navigate to" /fb/foo/bar
        // then the Request.Url.Query will be as follows after the 404 error: ?aspxerrorpath=/fb/foo/bar
        var requestedPath = Request.RequestContext.HttpContext.Request.QueryString["aspxerrorPath"];
    
        if (requestedPath.StartsWith("/fb/", StringComparison.OrdinalIgnoreCase))
        {
            var requestedUrl = Request.RequestContext.HttpContext.Request.Url;
            var pathAndQuery = requestedUrl.PathAndQuery;
            var absolutePath = requestedUrl.AbsolutePath;
    
            var mungedVirtualPath = pathAndQuery.Replace(absolutePath, "/ErrorPage404FB.aspx");
    
            Response.Redirect(mungedVirtualPath);
        }
    
        base.OnInit(e);
    }
    

    Request.RequestContext.HttpContext.Request 实际上返回的实例与简单的 Request 不同吗?

    【讨论】:

    • 所以你是说我应该让来自 web.config 的重定向发生,然后如果我处于 Facebook 模式,则从 ErrorPage.aspx 执行另一个重定向或 server.transfer()?
    • 实际上,我们的应用程序中根本不使用 Session。它已关闭,所以这不是我们的选择。
    • 您好,看看我下面的建议,您可以检查 URL 引荐来源网址,然后决定是否要重定向到新的错误页面
    • 那么您如何跟踪应在多个请求中显示哪个母版页?
    • 来自 Facebook 应用程序的请求将始终采用以下形式:apps.facebook.com/ipredikt/fb... 我将设置 ASP.NET 路由,将这个请求映射到:defaultfb.aspx。反过来,该页面知道它是 Facebook 页面,因此它为构建 UI 的所有 ASCX 控件设置适当的“IsFacebookPage”变量。正如我所指出的,是页面本身知道它们是否是 Facebook 模式页面,所以我不能在 Application 范围内设置它。
    【解决方案4】:

    最简单的方法是使用会话,但如果您在网站上不使用会话,您始终可以使用 cookie,当用户到达错误页面时检查 cookie 并决定是否要将他重定向到新的错误页面

    【讨论】:

    • 伙计,要显示 Facebook 特定版本的错误页面似乎需要做很多工作。
    • UrlReferrer 不可用。它是空的。我猜这是因为 web.config 中的设置。看起来唯一能给我线索的字符串是基于:Request.RequestContext.HttpContext.Request.Url。我回来了:localhost:3737/ErrorPage404.aspx?aspxerrorpath=/fb/foo/bar 所以我可以查看“aspxerrorpath”的值,将其拆分为“/”,然后查看元素零是否为“fb”。如果是,则执行 Server.Transfer。
    猜你喜欢
    • 2010-09-30
    • 1970-01-01
    • 2016-05-12
    • 2010-12-07
    • 2010-11-17
    • 2011-04-17
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    相关资源
    最近更新 更多