如何在运行时覆盖 web.config customErrors 设置?

发布于 2024-11-16 08:25:35 字数 1241 浏览 3 评论 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>

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

I have the following code in the web.config file for my ASP.NET C# app that's hosed on Azure:

<!-- 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>

This works great for errors when I'm hitting my site site natively (http://ipredikt.com/ErrorPage.aspx), but I also have a Facebook version of the app in which all of the pages use a different MasterPage and hence a different URL (http://ipredikt.com/ErrorPageFB.aspx).

Is it possible to modify the customError redirect values at runtime when I'm running in Facebook app mode, as if I had the following settings in web.config:

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

I don't think I can set this at the Application scope since it's individual pages in my app that have knowledge of whether they are running in Facebook mode.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧伤慢歌 2024-11-23 08:25:35

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

Hi I think what you can do is make another redirect inside your custom error page acording to the referrer - Request.UrlReferrer sometime the referrer is null so make sure you deal with that

人生戏 2024-11-23 08:25:35

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

不太理想,但它有效。

So here's a brute force solution. I'm using this on the page for the non-Facebook mode 404 errors:

  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);
  }

Hardly ideal, but it works.

菩提树下叶撕阳。 2024-11-23 08:25:35

“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.RequestContext.HttpContext.Request 不同的实例代码>请求?

"Facebook mode" seems like something you could track in Session, which would be accessible in ErrorPage.aspx to trigger a transfer to ErrorPageFB.aspx.

Update - you can clean up your brute-force solution quite a bit by using 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);
}

Does Request.RequestContext.HttpContext.Request actually return a different instance than simply Request?

原来分手还会想你 2024-11-23 08:25:35

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

The easy way to do it is using session but if you don't use session on you website you can always use cookie and when the user arrive to the error page examine the cookie and decide if you want to redirect him to a new error page

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文