2014-03-12 38 views
1

我知道REST应该是无状态的。在Web Api 2中启用会话

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

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

回答

4

在Global.asax中从this question

被盗增加以下内容:

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

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