Accessing Session Using ASP.NET Web API

前端 未结 13 961
谎友^
谎友^ 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;
    }
    
    <input type="hidden" value="@sessionValue" id="hBlah" />
    

    Javascript

    $(document).ready(function () {

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

    }

    0 讨论(0)
提交回复
热议问题