首先打开项目的Global.asax文件,重新方法init
public override void Init()
{
//注册事件
this.AuthenticateRequest += WebApiApplication_AuthenticateRequest;
base.Init();
}
private void WebApiApplication_AuthenticateRequest(object sender, EventArgs e)
{
//启用 webapi 支持session 会话
HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required);
}
如此就启用了。可以再代码中使用
如:
HttpContext.Current.Session["UserId"]
大家如果有问题可以
Console.WriteLine(”加群“+"537905238");