2012-07-13 79 views
0

會話Session_Start調用和ActionFilterAttribute中的OnActionExecuting之間會發生什麼。在ASP.NET MVC中正在清除的會話

出於某種原因,當我設置是這樣的:

protected void Session_Start(object sender, EventArgs e) 
{ 
    Session["GoToBuyPage"] = true; 
} 

,並嘗試在這裏訪問的ActionFilterAttribute:

public override void OnActionExecuting(ActionExecutingContext filterContext) 
{ 
    bool goToPage = (bool)Session["GoToBuyPage"]; 

它始終是零。任何想法爲什麼?

回答

0

有沒有Session財產ActionFilterAttribute。所以我甚至不知道你的代碼是如何編譯的。以下作品完美的罰款對我來說:

行動過濾器:

public class FooAttribute: ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     bool goToPage = (bool)filterContext.HttpContext.Session["GoToBuyPage"]; 
     filterContext.Result = new ContentResult 
     { 
      Content = goToPage.ToString() 
     }; 
    } 
} 

控制器:

public class HomeController : Controller 
{ 
    [Foo] 
    public ActionResult Index() 
    { 
     return View(); 
    } 
} 

Session_Start

protected void Session_Start(object sender, EventArgs e) 
{ 
    Session["GoToBuyPage"] = true; 
}