WebApi如何启用Session并且使用

首先打开项目的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");

posted @ 2019-08-29 15:08  Sqsdhc  阅读(25)  评论(0)    收藏  举报