3

I need to store a large object in Session Data in a Web API project. Then I need, a couple HTML widgets on the client want to access that data, in session of the Web API, and do different things with it, like a charting widget that graphs the data, an HTML table that lists the data. Rather than having to re-compute that large object for each client widget, I need to compute it ONCE and store it in session data, so that any widget running on the client within the same session can access that data at different times within the session and not have to wait for the server to re-compute the data.

Each widget should access the Web API server project using a different GET request url.

I appreciate the help. I have MAYBE a high level overview of how to do this but please I could use some guidance.

Logan H
  • 3,383
  • 2
  • 33
  • 50

2 Answers2

7

Here is how you can do it.

You may have Repository(Repository Pattern) in your project and inside Repository you have method that is computing your desired results for the widgets etc. Define your all logic of Data computation in your Repository Method and then inside your apiController Get Request or GetById you can call that Repository and can create a Session Variable which you can use in Your View.If you are using Asp.Net Mvc for your front end you can do the same call of storing data into sessions inside your Mvc Controller

public class YourControllerName:apiController
{
    public IQueryable<AnyNameListDTO> Get()
    {
        //Your other Code will go here 
        var session = HttpContext.Current.Session;
        if (session != null)
        {
            if (session["AnyName"] == null)
            {
                session["AnyName"] = TheRepository.YourMethodName();
            }
        }
    }
}

If you are using your Asp.Net Mvc as for your Views(UI) which will consume Web Api then you can create same sessions inside your controllers Actions methods

public class YourControllerName:Controller
{
    public ActionResult Get()
    {
        //Your other Code will go here 
        Session["AnyName"] = TheRepository.YourMethodName();
    }
}   

You can also use HTML5 Local Storage as well.

Utilize HTML5 local storage store data into that and then use it.

Dan Beaulieu
  • 19,406
  • 19
  • 101
  • 135
Husrat Mehmood
  • 2,270
  • 1
  • 20
  • 22
  • +1 for HTML5 Local Storage, which is probably the best place to store larges amounts of data for each client. Why clog your server's memory up when you have a whole computer or smart device on the other end that has plenty of spare memory? – mason Sep 11 '14 at 01:38
  • 100% Agree with you HTML5 local Storage I just told him all possible options – Husrat Mehmood Sep 11 '14 at 02:47
  • @ProgrammingNinja How would I access the session data from an HTML page that ISN'T in the Web API? (Just a stand alone HTML page) I think that is what I need the get request URL for – Logan H Sep 11 '14 at 21:30
  • I have suggested two places one is apiController and second is inside your Mvc Controller if you have to access Web Api session inside your Mvc Controller you can do as follows First create HttpBaseContext HttpContextBase Context; then access session var value = this.Context.Session["anyKey"]; second is you define session inside your Mvc Controller inside your View you would have to use Session["anyKey"] say you want to assign a session to a variable in your view you can do it as follows @{ var sessionData=Session["anyKey"];} you can also call Session["anyKey"] directly as well. – Husrat Mehmood Sep 12 '14 at 03:01
  • @ProgrammingNinja I see, but I need another HTML client, not in the Web API views, to retrieve that data that is in the Web API session. I did some research and found CORS (Cross Origin Resource Sharing) but I don't know if all that is necessary? – Logan H Sep 12 '14 at 15:55
  • plz see this answer link http://stackoverflow.com/questions/9594229/accessing-session-using-asp-net-web-api – Husrat Mehmood Sep 12 '14 at 16:21
  • How to get session value in mvccontroller from apicontroller ? – Aiyoub A. Jan 30 '18 at 10:25
2

SOLVED

Retrieving Web API Session data from a cross domain .html page

Cross Domain Resource Sharing is what this is

ValuesController.cs (IN the Web API)

namespace TestWEB_API.Controllers
{
  public class ValuesController : ApiController
  {

    // GET api/values
    public string Get()
    {
        var session = HttpContext.Current.Session;
        if (session != null)
        {
            if (session["Time"] == null)
                session["Time"] = DateTime.Now;
            return "Session Time: " + session["Time"];
        }
        return "Session is not working";
    }// End Get()

    ...

  }//End Class()
}//End Namespace

Then .html page (NOT IN THE WEB API!!!) In a different .war

httpSessionTest.html

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>

    <script type="text/javascript" src="../Desktop/jquery-2.1.1.js"></script>

</head>
<script>
// !!------------- THIS IS WHAT I NEEDED! ---------------!!
$(function()
{
        $.getJSON('http://localhost:Port/api/values', function(stringPayLoad)
        {
            alert(stringPayLoad);
            $("#dataP").text(stringPayLoad);
        });
});

</script>
<body>
<h3> Web API Session Test </h3>
<p id="dataP">!!! If you see this, it means that the session data didn't load. :(</p>

</body>
</html>
Logan H
  • 3,383
  • 2
  • 33
  • 50