在ASP.NET MVC中清除会话

时间:2012-07-13 21:48:17

标签: asp.net-mvc filter onactionexecuting

ActionFilterAttribute中的Session_Start调用和OnActionExecuting之间会话会发生什么。

出于某种原因,当我设置这样的东西时:

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

并尝试在ActionFilterAttribute中访问它:

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

它始终为null。有什么想法吗?

1 个答案:

答案 0 :(得分: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;
}
相关问题