2013-06-05 25 views
0

我需要爲某些URL執行301重定向。以下是代碼的骨架RouteBase中的會話爲空:: GetRouteData

public override RouteData GetRouteData(HttpContextBase httpContext) 
    { 
    . 
    . 
    . 

     if (newUrl.Length > 0) 
     { 
      response.Status = "301 Moved Permanently"; 
      response.RedirectLocation = newUrl; 
      response.End(); 
     } 
    } 

這很好。它重定向正確的某些網址。但是,我想要一種方法來檢測下一個請求中發生301重定向。所以我希望使用一個標誌作爲會話變量的一部分。類似這樣的:

httpContext.Session["RedirectHappened"] = true; 

但會話對象爲空。我如何訪問會話?或者有沒有其他的方法來檢測當前的200 OK請求是否是以前301重定向的結果。

謝謝。

回答

0

我們在相應的控制器中使用MVC的RedirectToActionPermanent修復了這個問題,我們使用Session對象設置了標誌。

public ActionResult Index() 
{ 
    Session["RedirectFromOldUrl"] = true; 

    return RedirectToActionPermanent("action", "controller"); 
}