【问题标题】:How to show a custom 404 page in ASP.NET without redirect?如何在没有重定向的情况下在 ASP.NET 中显示自定义 404 页面?
【发布时间】:2010-11-24 23:05:43
【问题描述】:

当 IIS 7 上的 ASP.NET 中的请求为 404 时,我希望显示自定义错误页面。地址栏中的 URL 不应更改,因此无需重定向。我该怎么做?

【问题讨论】:

  • 你有很多标签 - 这是网络表单还是 MVC?

标签: c# .net asp.net asp.net-mvc vb.net


【解决方案1】:

你可以使用

Server.Transfer("404error.aspx")

【讨论】:

    【解决方案2】:

    我使用 http 模块来处理这个问题。它适用于其他类型的错误,而不仅仅是 404,并允许您继续使用自定义错误 web.config 部分来配置显示哪个页面。

    public class CustomErrorsTransferModule : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.Error += Application_Error;
        }
    
        public void Dispose()  {  }
    
        private void Application_Error(object sender, EventArgs e)
        {
            var error = Server.GetLastError();
            var httpException = error as HttpException;
            if (httpException == null)
                return;
    
            var section = ConfigurationManager.GetSection("system.web/customErrors") as CustomErrorsSection;
            if (section == null)
                return;
    
            if (!AreCustomErrorsEnabledForCurrentRequest(section))
                return;
    
            var statusCode = httpException.GetHttpCode();
            var customError = section.Errors[statusCode.ToString()];
    
            Response.Clear();
            Response.StatusCode = statusCode;
    
            if (customError != null)
                Server.Transfer(customError.Redirect);
            else if (!string.IsNullOrEmpty(section.DefaultRedirect))
                Server.Transfer(section.DefaultRedirect);
        }
    
        private bool AreCustomErrorsEnabledForCurrentRequest(CustomErrorsSection section)
        {
            return section.Mode == CustomErrorsMode.On ||
                   (section.Mode == CustomErrorsMode.RemoteOnly && !Context.Request.IsLocal);
        }
    
        private HttpResponse Response
        {
            get { return Context.Response; }
        }
    
        private HttpServerUtility Server
        {
            get { return Context.Server; }
        }
    
        private HttpContext Context
        {
            get { return HttpContext.Current; }
        }
    }
    

    以与任何其他模块相同的方式在您的 web.config 中启用

    <httpModules>
         ...
         <add name="CustomErrorsTransferModule" type="WebSite.CustomErrorsTransferModule, WebSite" />
         ...
    </httpModules>
    

    【讨论】:

      【解决方案3】:

      作为通用的 ASP.NET 解决方案,在 web.config 的 customErrors 部分中,添加 redirectMode="ResponseRewrite" 属性。

      <customErrors mode="On" redirectMode="ResponseRewrite">
        <error statusCode="404" redirect="/404.aspx" />
      </customErrors>
      

      注意:这在内部使用 Server.Transfer(),因此重定向必须是网络服务器上的实际文件。不能是 MVC 路由。

      【讨论】:

      • NB 这是在 3.5 SP1 中添加的。
      • 我有一个网页抛出 InvalidOperationException 导致返回 500 错误。但是,当我添加上面的配置sn -p时,显示错误页面,但是HTTP状态码变成了200。使用ASP.NET 4.0和4.5。
      • 是的,解决方法是在 404.aspx.cs 中的重写 Render 方法中手动设置 Response.StatusCode。可能有更好的解决方案,但这似乎可行。
      • 那么如果必须是静态文件,为什么要重定向到 ASPX 页面:-)
      • 它没有。该文件必须存在于文件系统上。我想您可以根据需要使用 HTML 文件。就我而言,ASPX 页面在错误页面中呈现动态内容。
      猜你喜欢
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 2018-01-18
      相关资源
      最近更新 更多