0

ActionFilterAttributeのSession_Start呼び出しとOnActionExecutingの間のSessionはどうなりますか。

どういうわけか、私がこのようなものを設定したとき:

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

ここでActionFilterAttributeからアクセスしてみてください。

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

常にnullです。なぜ何かアイデアはありますか?

4

1 に答える 1

-1

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;
}
于 2012-07-14T09:46:13.567 に答える