WebAPI 2 attribute routing enable session state

前端 未结 3 1864
鱼传尺愫
鱼传尺愫 2021-01-02 04:22

We figured out how to enable session state with webapi Sample here

Now we have WebApi 2 attribute routing, so we no longer have route object to inject custom handler

相关标签:
3条回答
  • 2021-01-02 04:48

    You can use the SessionStateUtility class to get the session state. Just call:

    var session = SessionStateUtility.GetHttpSessionStateFromContext(HttpContext.Current)
    

    Api controllers are designed for restful services and should generally be stateless. Not loading the session every time is one of the things that makes them lighter weight.

    0 讨论(0)
  • 2021-01-02 04:52

    You need to add this to global.asax

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

    Then you could access the session through:

    HttpContext.Current.Session
    
    0 讨论(0)
  • 2021-01-02 04:52

    in the global.asax

    Private Sub WebApiApplication_PostAuthorizeRequest(sender As Object, e As EventArgs) Handles Me.PostAuthorizeRequest
         System.Web.HttpContext.Current.SetSessionStateBehavior(System.Web.SessionState.SessionStateBehavior.Required)
    End Sub
    
    0 讨论(0)
提交回复
热议问题