如何在运行时覆盖 web.config customErrors 设置?
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您好,我认为您可以做的是根据引用者 - 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
所以这是一个蛮力解决方案。我在页面上使用这个来处理非 Facebook 模式 404 错误:
不太理想,但它有效。
So here's a brute force solution. I'm using this on the page for the non-Facebook mode 404 errors:
Hardly ideal, but it works.
“Facebook 模式”似乎可以在 Session 中跟踪,可以在 ErrorPage.aspx 中访问它以触发到 ErrorPageFB.aspx 的传输。
更新 - 您可以使用 Request.QueryString 来清理您的暴力解决方案:
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
:Does
Request.RequestContext.HttpContext.Request
actually return a different instance than simplyRequest
?最简单的方法是使用 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