WebApi中使用session

时间:2023-03-08 18:16:24
WebApi中使用session

webapi默认是不支持session的,要通过一些手动配置来开启Session功能

在Global.asax里添加:

导入命名空间:

using System.Web.SessionState;
public class WebApiApplication : System.Web.HttpApplication
{
public override void Init()
{
this.PostAuthenticateRequest += (sender, e) => HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
base.Init();
}