转:https://blog.csdn.net/lxrj2008/article/details/74164222
默认webapi 不开启 session 会话支持
所以需要修改配置,手动在 Global 开启 session 支持
1.重写 init() 方法
2.开启session支持
Global.asax
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace WebApiCORS
{
public class WebApiApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
public override void Init()
{
//注册事件
this.AuthenticateRequest += WebApiApplication_AuthenticateRequest;
base.Init();
}
//开启session支持
void WebApiApplication_AuthenticateRequest(object sender, EventArgs e)
{
//启用 webapi 支持session 会话
HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required);
}
}
}