为什么无法使用asp.net web api访问会话?

为什么无法使用asp.net web api访问会话?,asp.net,session,asp.net-web-api2,Asp.net,Session,Asp.net Web Api2,在我的ASP.NET Web API项目中,我可以访问本地(调试和发布模型)中的会话对象 但是当我把它部署到服务器上时,它就不起作用了 Global.asax public override void Init() { this.PostAuthenticateRequest +=MvcApplication_PostAuthenticateRequest; base.Init(); } void MvcApplication_PostAuthenticateRequest(o

在我的ASP.NET Web API项目中,我可以访问本地(调试和发布模型)中的会话对象

但是当我把它部署到服务器上时,它就不起作用了

Global.asax

public override void Init()
{
    this.PostAuthenticateRequest +=MvcApplication_PostAuthenticateRequest;
    base.Init();
}

void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
{
    System.Web.HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
}  
UserApiController.cs

    [HttpGet]
    public string GetVerificationCode(string mobileNumber)
    {
        if (!string.IsNullOrWhiteSpace(mobileNumber) &&
             Regex.Match(mobileNumber, @"^1\d{10}$", RegexOptions.IgnoreCase).Success)
        {
            string VerificationCode = "1234";
            if (HttpContext.Current.Session != null)
            {
                HttpContext.Current.Session["VerificationCode"] = VerificationCode;
            }
            return VerificationCode;
        }
        throw new ArgumentException("Phone number format is incorrect");
    }

    [HttpGet]
    public string GetSessionString()
    {
        if (HttpContext.Current.Session != null)
        {
            return HttpContext.Current.Session["VerificationCode"].ToString();
        }
        return string.Empty;
    }

为什么它不工作?

你说的“它不工作”是什么意思?它会引发异常吗?一般来说,会话状态默认通过cookie/HttpCache实现;常见错误是,如果生产环境中有多台服务器,则需要会话状态提供程序来跨多台服务器同步会话状态。将其部署到服务器时,
HttpContext.Current.session
始终为空。是否可以在生产环境中检查web.config/machine.config?可以通过web.config或machine.config关闭会话状态。谢谢,我还没有更改任何内容,现在我可以使用会话了。