2008-12-07 128 views
74

嘗試爲Google網站管理員工具設置我的網站後,我發現我的自定義ASP.NET 404頁面未返回404狀態碼。它顯示了正確的自定義頁面,並告訴瀏覽器一切正常。這是考慮軟404或虛假404。谷歌不喜歡這個。所以我發現了很多關於這個問題的文章,但我想要的解決方案似乎並不奏效。ASP.NET自定義404返回200確定而不是404未找到

我想要的解決方案是將以下兩行添加到自定義404頁面的Page_Load方法後面的代碼中。

Response.Status = "404 Not Found"; 
Response.StatusCode = 404; 

這是行不通的。該頁面仍然返回200 OK。然而,我發現如果我將以下代碼硬編碼到設計代碼中,它將正常工作。

<asp:Content ID="ContentMain" ContentPlaceHolderID="ContentPlaceHolderMaster" runat="server"> 

<% 
    Response.Status = "404 Not Found"; 
    Response.StatusCode = 404; 
%> 

... Much more code ... 

</asp:content> 

該頁面使用母版頁。我正在配置web.config中的自定義錯誤頁面。我真的更願意使用後面的代碼,但是我似乎無法在設計/佈局中不使用黑客內聯代碼的情況下工作。

+0

瀏覽器狀態如何?我使用Firefox的插件Header Spy。 – 2008-12-07 06:17:25

+0

標頭間諜響應: HTTP/1.1 404未找到 日期:Sun,07 Dec 2008 06:21:20 GMT – 2008-12-07 06:21:54

+0

您是否正在使用母版頁?也許就是這樣。我會嘗試一個頁面,而不使用母版頁... – 2008-12-07 06:23:12

回答

68

解決方案:

的問題,事實證明,是使用母版頁。我通過稍後在頁面生命週期中設置狀態碼來工作,顯然母版頁的渲染重置了它,所以我重寫渲染方法並在渲染完成後設置它。

protected override void Render(HtmlTextWriter writer) 
{ 
    base.Render(writer); 
    Response.StatusCode = 404; 
} 

更多的工作可以做,以找出究竟當母版頁設置狀態,但我會留給你。


原貼:

我能得到一個測試web應用程序正常工作,那麼它至少顯示自定義錯誤頁,並返回404個狀態碼。我不能告訴你什麼是錯了你的應用程序,但我可以告訴你,我做了什麼:

1)編輯自定義錯誤web.config中:

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

2)增加了一個404.aspx頁面和狀態代碼設置爲404

public partial class _04 : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     Response.StatusCode = 404; 
    } 
} 

那一下,如果我去到任何頁面EXTE由Asp.Net處理,不存在nsion,我拉琴日誌清楚地顯示出一個404,這裏是標題:

HTTP/1.1 404 Not Found 
Server: Microsoft-IIS/5.1 
Date: Sun, 07 Dec 2008 06:04:13 GMT 
X-Powered-By: ASP.NET 
X-AspNet-Version: 2.0.50727 
Cache-Control: private 
Content-Type: text/html; charset=utf-8 
Content-Length: 533 

現在,如果我去不是由天冬氨酸處理的頁面.Net,就像一個htm文件一樣,自定義頁面不顯示,並且顯示由IIS配置的404。

這裏有一些可能對你和你的問題有用的更多細節,我的測試確實做了一個重定向到新頁面,所以請求的文件的URL幾乎丟失了(除了在查詢字符串)。

Google 404 and .NET Custom Error Pages

頭間諜響應:

HTTP/1.1 404 Not Found 
Date: Sun, 07 Dec 2008 06:21:20 GMT 
5

經過大量的測試和故障排除出現某些託管服務提供商可以返回代碼干擾。我能夠通過在內容中應用「黑客」來解決這個問題。

<% 
// This code is required for host that do special 404 handling... 
Response.Status = "404 Not Found"; 
Response.StatusCode = 404; 
%> 

這將允許網頁無論什麼返回正確的返回代碼。

27

我有一個類似的問題,我想顯示自定義頁面爲404(這是ASPX),它能正常工作在本地主機,但只要遠程連接訪問者他們將得到通用的IIS 404

這個問題的解決是添加

Response.TrySkipIisCustomErrors = true; 

改變Response.StatusCode之前。

通過裏克施特拉爾找到http://www.west-wind.com/weblog/posts/745738.aspx

9

嘗試到Response.End調用()跳過渲染...

Response.Status = "404 Not Found"; 
Response.StatusCode = 404; 
Response.End(); 
return; 
12

的IIS 7解決方法就是添加到您的web.config文件:

<system.webServer> 
    <httpErrors existingResponse="Replace"> 
    <remove statusCode="500" subStatusCode="-1" /> 
    <remove statusCode="404" subStatusCode="-1" /> 
    <error statusCode="404" prefixLanguageFilePath="" path="404.htm" responseMode="File" /> 
    <error statusCode="500" prefixLanguageFilePath="" path="500.htm" responseMode="File" /> 
    </httpErrors> 
</system.webServer> 

http://forums.asp.net/t/1563128.aspx/1

0

我是能夠得到解決通過使用.NET 3.5的asp.net webforms中的以下設置來解決此問題。

我實現的模式繞過在web.config .NET的自定義重定向解決方案,我寫我自己來處理所有的場景頭中具有正確的HTTP狀態代碼。

首先,web.config中的部分的customErrors看起來是這樣的:

<customErrors mode="RemoteOnly" defaultRedirect="~/error.htm" /> 

這種設置保證的customErrors模式設置爲上,後來我們就需要設置,並提供了一個所有其他人,失敗選項爲error.htm的defaultRedirect。這會派上用場的時候我沒有特定的錯誤處理程序,或有沿着破碎的數據庫連接線的東西。

其次,這裏是全球ASAX錯誤事件:

protected void Application_Error(object sender, EventArgs e) 
    { 
     HandleError(); 
    } 

    private void HandleError() 
    { 
     var exception = Server.GetLastError(); 
     if (exception == null) return; 

     var baseException = exception.GetBaseException(); 

     bool errorHandled = _applicationErrorHandler.HandleError(baseException); 
     if (!errorHandled) return; 


     var lastError = Server.GetLastError(); 
    if (null != lastError && HttpContext.Current.IsCustomErrorEnabled) 
    { 
     Elmah.ErrorSignal.FromCurrentContext().Raise(lastError.GetBaseException()); 
     Server.ClearError(); 
    } 
    } 

此代碼是假冒的處理錯誤給另一大類責任。如果不處理的錯誤,的customErrors被打開,這意味着我們已經有了,我們是生產和莫名其妙的錯誤沒有被處理的情況。我們會在這裏清除,以防止用戶看到它,但它登錄在ELMAH所以我們知道這是怎麼回事。

的applicationErrorHandler類看起來是這樣的:

public bool HandleError(Exception exception) 
     { 
      if (exception == null) return false; 

      var baseException = exception.GetBaseException(); 

      Elmah.ErrorSignal.FromCurrentContext().Raise(baseException); 

      if (!HttpContext.Current.IsCustomErrorEnabled) return false; 

      try 
      { 

       var behavior = _responseBehaviorFactory.GetBehavior(exception); 
       if (behavior != null) 
       { 
        behavior.ExecuteRedirect(); 
        return true; 
       } 
      } 
      catch (Exception ex) 
      { 
       Elmah.ErrorSignal.FromCurrentContext().Raise(ex); 
      } 
      return false; 
     } 

該類基本上使用命令模式來定位該款項核發錯誤的類型相應的錯誤處理程序。在這個級別使用Exception.GetBaseException()是很重要的,因爲幾乎每個錯誤都會被封裝在一個更高級別的異常中。例如,從任何aspx頁面執行「throw new System.Exception()」將導致在此級別接收到HttpUnhandledException,而不是System.Exception。

「工廠」的代碼很簡單,看起來像這樣:

public ResponseBehaviorFactory() 
    { 
     _behaviors = new Dictionary<Type, Func<IResponseBehavior>> 
         { 
          {typeof(StoreException),() => new Found302StoreResponseBehavior()}, 
          {typeof(HttpUnhandledException),() => new HttpExceptionResponseBehavior()}, 
          {typeof(HttpException),() => new HttpExceptionResponseBehavior()}, 
          {typeof(Exception),() => new Found302DefaultResponseBehavior()} 
         }; 
    } 

    public IResponseBehavior GetBehavior(Exception exception) 
    {                    
     if (exception == null) throw new ArgumentNullException("exception"); 

     Func<IResponseBehavior> behavior; 
     bool tryGetValue = _behaviors.TryGetValue(exception.GetType(), out behavior); 

     //default value here: 
     if (!tryGetValue) 
      _behaviors.TryGetValue(typeof(Exception), out behavior); 

     if (behavior == null) 
      Elmah.ErrorSignal.FromCurrentContext().Raise(
       new Exception(
        "Danger! No Behavior defined for this Exception, therefore the user might have received a yellow screen of death!", 
        exception)); 
     return behavior(); 
    } 

最後,我有一個可擴展的錯誤處理方案設置。在每個定義的「行爲」中,我都有一個針對錯誤類型的自定義實現。例如,將檢查一個Http異常的狀態代碼並正確處理。 404狀態代碼將需要Server.Transfer而不是Request.Redirect,以及寫入標題中的相應狀態代碼。

希望這會有所幫助。

相關問題