Mvc WebApi 中支持Session,需要如下设置

0
884

1、Global.asax 文件中重写Init方法来指定会话需要支持的类型(MVC的WebApi中默认是没有开启Session会话支持的)


//初始化开启Session
public override void Init()
{
this.PostAuthenticateRequest += (sender, e) =>
{
HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required);
};
base.Init();
}

2、using System.Web.SessionState;

3、 public class AccountService:IRequiresSessionState  继承 IRequiresSessionState接口

4、设置Session缓存

HttpContext.Current.Session["Hello"] = "Word";
然后一切OK了。

回复

请输入你的评论!
请在这里输入您的名字