代码之家  ›  专栏  ›  技术社区  ›  Muhammad Saqlain

在ApiController中访问WebForms ASPX页会话

  •  2
  • Muhammad Saqlain  · 技术社区  · 6 年前

    我正在试图访问作为WebForm项目一部分的控制器中的会话。所有会话变量都设置在ASPX页上。

    Session["SESSION_KEY_UI_CULTURE"] = ddlLanguage.SelectedValue;
    

    现在,根据客户机的API请求,我需要读取会话变量并执行任务,但会话始终为空。

    [RoutePrefix("api/accounts")]
        public class AccountsController : ApiController
        {
            [Route("config")]
            [HttpGet]
            public IHttpActionResult GetCompanyConfig()
            {
                if(HttpContext.Current.Session != null)
                {
                    //Session is always NULL
                }
    
                return Ok();
            }
        }
    

    我尝试在webconfig文件中删除和添加sessionstatemodule

    <remove name="Session" />
    <add name="Session" type="System.Web.SessionState.SessionStateModule"/>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Severin Jaeschke    6 年前

    protected void Application_PostAuthorizeRequest() {
     HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
    }
    

    Sub Application_PostAuthorizeRequest()
        HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required)
    End Sub
    

    Here here