Accessing Session Using ASP.NET Web API

前端 未结 13 943
谎友^
谎友^ 2020-11-22 04:32

I realize session and REST don\'t exactly go hand in hand but is it not possible to access session state using the new Web API? HttpContext.Current.Session is a

13条回答
  •  终归单人心
    2020-11-22 05:30

    Going back to basics why not keep it simple and store the Session value in a hidden html value to pass to your API?

    Controller

    public ActionResult Index()
            {
    
                Session["Blah"] = 609;
    
                YourObject yourObject = new YourObject();
                yourObject.SessionValue = int.Parse(Session["Blah"].ToString());
    
                return View(yourObject);
            }
    

    cshtml

    @model YourObject
    
    @{
        var sessionValue = Model.SessionValue;
    }
    
    
    

    Javascript

    $(document).ready(function () {

        var sessionValue = $('#hBlah').val();
    
        alert(sessionValue);
    
        /* Now call your API with the session variable */}
    

    }

提交回复
热议问题