2012-07-13 45 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; 
}