2015-07-12 54 views
0

我配置我的自定義錯誤Global.asax中是這樣的:ASP.Net AJAX頁面方法和自定義HTTP錯誤(Web窗體)

protected void Application_Error(object sender, EventArgs e) 
    { 
     Exception exc = Server.GetLastError(); 

     if (exc.GetType() == typeof(HttpException)) 
     { 
      var http_ex = exc as HttpException; 

      var error_code = http_ex.GetHttpCode(); 

      if (error_code == 404) 
      { 
       Response.Redirect("~/404.aspx", true); 
      } 
      if (error_code == 500) 
      { 
       Response.Redirect("~/500.aspx", true); 
      } 
     } 
    } 

這是工作的罰款。

後來在我的開發中,我不得不使用Ajax Page Method。對於這一點,我添加了ScriptModule處理程序在web.config:

<modules> 
    <remove name="ScriptModule" /> 
    <add name="ScriptModule" preCondition="managedHandler" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 
</modules> 

但現在,404錯誤回落至醜陋的IIS默認的404頁面。

我在Global.asax添加一個斷點,在這一行:

Response.Redirect("~/404.aspx", true); 

和斷點以及觸發。當我在這一行上點擊F10時,它立即重定向到醜陋的IIS 404頁面。 (錯誤是針對請求的頁面,而不是針對404.aspx頁面)。

Web服務器是從Visual Studio 2013的IIS Express。我的web.config中沒有其他處理程序或模塊。我正在運行Asp.Net Web Forms,Framework 3.5。

你能解釋一下爲什麼,和/或給我解決這個奇怪的行爲?

回答

0

我終於找到了托馬斯馬夸特的博客帖子的答案在這裏: http://blogs.msdn.com/b/tmarq/archive/2009/06/25/correct-use-of-system-web-httpresponse-redirect.aspx

我加Server.ClearError(),這裏是我現在的Application_Error:

protected void Application_Error(object sender, EventArgs e) 
{ 
    Exception exc = Server.GetLastError(); 

    if (exc.GetType() == typeof(HttpException)) 
    { 
     Server.ClearError(); 

     var http_ex = exc as HttpException; 

     var error_code = http_ex.GetHttpCode(); 

     if (error_code == 404) 
     { 
      Response.Redirect("~/404.aspx", true); 
     } 
     if (error_code == 500) 
     { 
      Response.Redirect("~/500.aspx", true); 
     } 
    } 
} 
+0

你會說,對此的解釋是正確的在博客文章中。現在我已經完全閱讀了,這是真的。看看你是否想要,這很有趣。 – KevinM