Web Api启用Session

Web Api默认是关闭Session

启用要在全局文件写入下面代码

        /// <summary>
        /// 启用Session(Web Api默认关闭Session)
        /// </summary>
        public override void Init()
        {
            PostAuthenticateRequest += MvcApplication_PostAuthenticateRequest;
            base.Init();
        }

        /// <summary>
        /// 启用Session(Web Api默认关闭Session)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
        {
            HttpContext.Current.SetSessionStateBehavior(
                System.Web.SessionState.SessionStateBehavior.Required);
        }

 

posted @ 2018-09-30 17:03  花生打代码会头痛  阅读(137)  评论(0)    收藏  举报