1、关于webApi使用session
在Global.asax中注册session添加以下代码
public override void Init() { //开启session this.PostAuthenticateRequest += (sender, e) => HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required); base.Init(); }
添加以后可以直接在webApi中使用session
[HttpGet] public HttpResponseMessage setSession() { HttpContext.Current.Session["AA"] = "AAAA"; return returnStringData("OK"); } [HttpGet] public HttpResponseMessage getSession() { HttpContext.Current.Session.Timeout = 100;//获取和设置的过期时间,以分钟为单位 //web.config也可以设置过期时间 //< system.web > // < sessionState mode = "InProc" timeout = "30" /> // </ system.web > return returnStringData(HttpContext.Current.Session["AA"].ToString()); }