Reputation: 3423
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.
Upvotes: 4
Views: 24511
Reputation: 3423
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>
Upvotes: 2
Reputation: 2310
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.
Upvotes: 7