Sasquatch3o3 Sasquatch3o3 - 1 month ago 18
ASP.NET (C#) Question

ASP.NET Web API Session Data

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.

Answer

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.