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 of HTML widgets on the client want to access this data, in a web API session and do different things with it, for example a graphical widget that graphically displays the data, an HTML table that lists the data. Instead of re-calculating this LOB for each client widget, I need to calculate it ONE and store it in the session data so that any widget running on the client within the same session can access that data at different times in the session, and no need to wait for the server to recount the data.

Each widget needs to access the web API server project using a different GET request url.

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

+3


source to share


2 answers


Here's how you can do it.

You can have a repository (repository pattern) in your project, and inside the repository, you have a method that calculates the desired results for widgets, etc. Define all the data calculation logic in your repository method, and then inside your apiController Get or GetById request, you can call that repository and create a session variable that you can use in your view. If you are using Asp.Net Mvc for your front you can make the same call to store data in 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) that will consume the Web Api, then you can create the same sessions inside your controllers. Action 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.

Use local HTML5 data stores and then use them.

+6


source


solved

Retrieving web API session data from cross domain .html page

Domain resource sharing is what it is

ValuesController.cs ( IN 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 WEB-API !!!) In different .war format

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>

      

+1


source







All Articles