0

我在 Azure 上安装的 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 ( http://ipredikt.com/ErrorPageFB.aspx )。

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

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

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

4

4 回答 4

1

“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

于 2011-06-20T06:17:00.937 回答
1

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

于 2011-06-20T06:20:23.010 回答
1

所以这是一个蛮力解决方案。我在页面上使用它来处理非 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);
  }

几乎不理想,但它有效。

于 2011-06-20T08:15:17.343 回答
0

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

于 2011-06-20T07:14:43.433 回答