2014-07-07 43 views
1

我有以下代碼檢查異常,如果它是404異常,它將檢查一個URL列表以查看頁面是否已被移動,如果匹配,將發出永久重定向新頁面:Does Response.RedirectPermanent();處理代碼後調用

protected void Application_Error(object sender, EventArgs e) 
    { 
     var exception = Server.GetLastError(); 
     if (exception != null) 
     { 
      if (exception is HttpException) 
      { 
       TryRedirect((HttpException)exception); 
      } 

      LogError(exception); 
     } 
    } 

    private void TryRedirect(HttpException httpException) 
    { 
     if (httpException.GetHttpCode() == 404) 
     { 
      WebsiteRedirect redirect = SiteCache.Redirects.FirstOrDefault(x => string.Compare(x.LegacyURL, HttpContext.Current.Request.RawUrl, true) == 0); 

      if (redirect != null) 
      { 
       // 301 it to the new url 
       Response.RedirectPermanent(redirect.NewURL); 
      } 
     } 
    } 

現在,我會想到的是,重定向發生後,該代碼的非後它會被執行即LogError功能不會被調用。但它似乎是因爲我得到的錯誤信息找不到網頁。

是對MVC Response.RedirectPermanent這個標準的行爲嗎?

回答

3

好了,所以事實證明出這是標準的行爲RedirectPermanent有2個重載(我從來沒有見過我的VS被打了第二個過載):

Response.RedirectPermanent(string url); 
Response.RedirectPermanent(string url, bool endResponse); 

endResponse選項說,永久重定向將繼續完成響應或結束該過程立即進行r重定向。

的默認設置爲false意味着在第一次過載(這是我用過的)將完成這就是爲什麼它被調用LogError功能

-1

當您使用response.RedirectPermanent()時,它將完全委託請求。它不會執行或處理Response.RedirectPermanent(redirect.NewURL)語句後的任何語句。 如果您使用的Response.RedirectPermanent(字符串,boolean)方法則賦予布爾值true,那麼它會執行你的LOGERROR(例外)

我請你去通過這個鏈接http://www.stepforth.com/blog/2008/redirects-permanent-301-vs-temporary-302/#.U7pxUfmSx-M

+0

那麼,爲什麼LOGERROR函數被調用的響應在RedirectPermanent之後? – Pete

+0

它永久重定向,即你是不是會得到控制,從那裏你叫 – Nadendla

+1

這是完全錯誤已經進一步讀入'RedirectPermanent'後面的請求時,它會繼續,除非你告訴它不處理響應 - 它需要一個如果將其設置爲true,將會終止當前響應。如果設置爲false,它將完成當前的響應。默認設置爲false – Pete

-1
Response.RedirectPermanent(string url, bool endResponse); 
return null; 
+4

我不明白這是如何回答「重定向被調用後會發生什麼」的問題。 – Teepeemm