【问题标题】:Display Custom Error Pages on IIS 7.5 and IIS 7在 IIS 7.5 和 IIS 7 上显示自定义错误页面
【发布时间】:2011-10-11 14:22:20
【问题描述】:

我在本地使用 Asp.net 4 C# 和 IIS 7,在生产服务器上使用 IIS 7.5。

我需要显示自定义错误页面。目前,我在 Global.asax 中使用了一些逻辑来绕过 IIS 默认页面。 在本地使用 IIS 7 我能够成功显示 CustomPages 但在生产 (IIS 7.5) 服务器默认 IIS 页面仍然存在。 我使用Response.TrySkipIisCustomErrors = true;,但在生产服务器上不起作用。

你能告诉我解决这个问题的方法吗?

我的代码在Global.Asax

Application_Error

Response.TrySkipIisCustomErrors = true;
                if (ex is HttpException)
                {
                    if (((HttpException)(ex)).GetHttpCode() == 404)
                    {

                        Server.Transfer("~/ErrorPages/404.aspx");
                    }
                }
                // Code that runs when an unhandled error occurs.
                Server.Transfer("~/ErrorPages/Error.aspx");

【问题讨论】:

  • 只是出于好奇,您为什么不让您的 web.config 处理自定义错误?如有必要,登录 application_error 并让配置处理下一个。更容易打开/关闭、配置等。
  • 亚当我做服务器传输以获得 404. Asp.net 自定义错误显示时默认显示 302 重定向
  • 她确实有,但为什么这对你来说是个问题呢?为谷歌返回一个真正的 404 状态?如果它针对最终客户,请注意,如果您的内容小于 500 字节左右,则 IE(7?)会显示内置的“友好”错误。奇怪的行为,所以只想检查一下这背后的原因。

标签: asp.net iis iis-7 iis-7.5


【解决方案1】:

我这样做的方式是在一个模块中,而不是在 Global.asax 中,并将其连接到标准的自定义错误内容中。试试这个:

public class PageNotFoundModule : IHttpModule
{
    public void Dispose() {}

    public void Init(HttpApplication context)
    {
        context.Error += new EventHandler(context_Error);
    }

    private void context_Error(object sender, EventArgs e)
    {
        var context = HttpContext.Current;

        // Only handle 404 errors
        var error = context.Server.GetLastError() as HttpException;
        if (error.GetHttpCode() == 404)
        {
            //We can still use the web.config custom errors information to decide whether to redirect
            var config = (CustomErrorsSection)WebConfigurationManager.GetSection("system.web/customErrors");

            if (config.Mode == CustomErrorsMode.On || (config.Mode == CustomErrorsMode.RemoteOnly && context.Request.Url.Host != "localhost"))
            {
                //Set the response status code
                context.Response.StatusCode = 404;

                //Tell IIS 7 not to hijack the response (see http://www.west-wind.com/weblog/posts/745738.aspx)
                context.Response.TrySkipIisCustomErrors = true;

                //Clear the error otherwise it'll get handled as usual
                context.Server.ClearError();

                //Transfer (not redirect) to the 404 error page from the web.config
                if (config.Errors["404"] != null)
                {
                    HttpContext.Current.Server.Transfer(config.Errors["404"].Redirect);
                }
                else
                {
                    HttpContext.Current.Server.Transfer(config.DefaultRedirect);
                }
            }
        }
    } 
}

【讨论】:

  • 我正在尝试,你能发布一个你的 Web.Config 的例子吗?谢谢
猜你喜欢
  • 2014-05-14
  • 1970-01-01
  • 2013-07-31
  • 1970-01-01
  • 1970-01-01
  • 2012-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多