在Web Api 2中启用会话

Ale*_*edo 4 c# asp.net-web-api

我知道REST应该是无状态的.

我的Web Api与我的MVC网站的同一个项目.我如何分享他们之间的会话?

我正在尝试使用Web Api 2的好东西并使用Ajax而不是构建RESTful API.

joe*_*dev 11

这个问题中偷来的

在global.asax中添加以下内容:

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

void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
{
    System.Web.HttpContext.Current.SetSessionStateBehavior(
        SessionStateBehavior.Required);
}
Run Code Online (Sandbox Code Playgroud)