Enable session in Web Api 2 [duplicate]

2020-03-05 02:06发布

I know REST should be stateless.

My Web Api is at the same project of my MVC Website. How can I share the session between them?

I'm trying to use the goodies of Web Api 2 and work with Ajax rather than build a RESTful API.

1条回答
聊天终结者
2楼-- · 2020-03-05 02:57

stolen from this question

in global.asax add the following:

public override void Init()
{
    this.PostAuthenticateRequest += MvcApplication_PostAuthenticateRequest;
    base.Init();
}

void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
{
    System.Web.HttpContext.Current.SetSessionStateBehavior(
        SessionStateBehavior.Required);
}
查看更多
登录 后发表回答