Async requests in ASP.NET MVC when working with Session and Partial Views












0














In my ASP.net application, multiple AJAX requests to the LoadPartialView controller action load the partial view directly inside the main view.



 public ActionResult LoadPartialView(string triggerParameter)
{
var myObject = GetData(triggerParameter);
SaveObjectToSession(myObject);
return PartialView("_PartialViewName", myObject);
}

public void SaveObjectToSession(object myObject)
{
_httpContextAccessor.HttpContext.Session.Set("objectKey", ValueToBytes(myObject));
}


My intention is to reuse the data returned by each call to the LoadPartialView in another controller action. I am trying to do so using HttpContext.Session, but it doesn't seem to work.



  public ActionResult Details(string triggerParameter)
{
if (_httpContextAccessor.HttpContext.Session.Get(triggerParameter) == null)
{
return RedirectToAction(nameof(Index));
}
....
}


I have tried to use the lock statement in the LoadPartialView like this:



 private static readonly object Lock = new object();

public ActionResult LoadPartialView(string triggerParameter)
{
lock(Lock) {
var myObject = GetData(triggerParameter);
SaveObjectToSession(myObject);
return PartialView("_PartialViewName", myObject);
}
}


but it doesn't work either.



Is it possible to re-use the data returned by the partial view action method in another controller action without needing to make the request to: GetData() twice?










share|improve this question
























  • That's an XY problem and not really related to sessions or partial views. What are you trying to do? Cache query results? Or view output? MVC has its own mechanisms for output caching. EF has its own mechanisms too, or you can cache results in the Cache object or the Session.
    – Panagiotis Kanavos
    Nov 12 at 9:51










  • this might help
    – Hooman Bahreini
    Nov 12 at 9:55










  • Both cache and view ouput. I am already caching results in the Session.
    – programmer44
    Nov 12 at 9:56
















0














In my ASP.net application, multiple AJAX requests to the LoadPartialView controller action load the partial view directly inside the main view.



 public ActionResult LoadPartialView(string triggerParameter)
{
var myObject = GetData(triggerParameter);
SaveObjectToSession(myObject);
return PartialView("_PartialViewName", myObject);
}

public void SaveObjectToSession(object myObject)
{
_httpContextAccessor.HttpContext.Session.Set("objectKey", ValueToBytes(myObject));
}


My intention is to reuse the data returned by each call to the LoadPartialView in another controller action. I am trying to do so using HttpContext.Session, but it doesn't seem to work.



  public ActionResult Details(string triggerParameter)
{
if (_httpContextAccessor.HttpContext.Session.Get(triggerParameter) == null)
{
return RedirectToAction(nameof(Index));
}
....
}


I have tried to use the lock statement in the LoadPartialView like this:



 private static readonly object Lock = new object();

public ActionResult LoadPartialView(string triggerParameter)
{
lock(Lock) {
var myObject = GetData(triggerParameter);
SaveObjectToSession(myObject);
return PartialView("_PartialViewName", myObject);
}
}


but it doesn't work either.



Is it possible to re-use the data returned by the partial view action method in another controller action without needing to make the request to: GetData() twice?










share|improve this question
























  • That's an XY problem and not really related to sessions or partial views. What are you trying to do? Cache query results? Or view output? MVC has its own mechanisms for output caching. EF has its own mechanisms too, or you can cache results in the Cache object or the Session.
    – Panagiotis Kanavos
    Nov 12 at 9:51










  • this might help
    – Hooman Bahreini
    Nov 12 at 9:55










  • Both cache and view ouput. I am already caching results in the Session.
    – programmer44
    Nov 12 at 9:56














0












0








0







In my ASP.net application, multiple AJAX requests to the LoadPartialView controller action load the partial view directly inside the main view.



 public ActionResult LoadPartialView(string triggerParameter)
{
var myObject = GetData(triggerParameter);
SaveObjectToSession(myObject);
return PartialView("_PartialViewName", myObject);
}

public void SaveObjectToSession(object myObject)
{
_httpContextAccessor.HttpContext.Session.Set("objectKey", ValueToBytes(myObject));
}


My intention is to reuse the data returned by each call to the LoadPartialView in another controller action. I am trying to do so using HttpContext.Session, but it doesn't seem to work.



  public ActionResult Details(string triggerParameter)
{
if (_httpContextAccessor.HttpContext.Session.Get(triggerParameter) == null)
{
return RedirectToAction(nameof(Index));
}
....
}


I have tried to use the lock statement in the LoadPartialView like this:



 private static readonly object Lock = new object();

public ActionResult LoadPartialView(string triggerParameter)
{
lock(Lock) {
var myObject = GetData(triggerParameter);
SaveObjectToSession(myObject);
return PartialView("_PartialViewName", myObject);
}
}


but it doesn't work either.



Is it possible to re-use the data returned by the partial view action method in another controller action without needing to make the request to: GetData() twice?










share|improve this question















In my ASP.net application, multiple AJAX requests to the LoadPartialView controller action load the partial view directly inside the main view.



 public ActionResult LoadPartialView(string triggerParameter)
{
var myObject = GetData(triggerParameter);
SaveObjectToSession(myObject);
return PartialView("_PartialViewName", myObject);
}

public void SaveObjectToSession(object myObject)
{
_httpContextAccessor.HttpContext.Session.Set("objectKey", ValueToBytes(myObject));
}


My intention is to reuse the data returned by each call to the LoadPartialView in another controller action. I am trying to do so using HttpContext.Session, but it doesn't seem to work.



  public ActionResult Details(string triggerParameter)
{
if (_httpContextAccessor.HttpContext.Session.Get(triggerParameter) == null)
{
return RedirectToAction(nameof(Index));
}
....
}


I have tried to use the lock statement in the LoadPartialView like this:



 private static readonly object Lock = new object();

public ActionResult LoadPartialView(string triggerParameter)
{
lock(Lock) {
var myObject = GetData(triggerParameter);
SaveObjectToSession(myObject);
return PartialView("_PartialViewName", myObject);
}
}


but it doesn't work either.



Is it possible to re-use the data returned by the partial view action method in another controller action without needing to make the request to: GetData() twice?







c# asp.net asp.net-mvc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 9:07

























asked Nov 12 at 9:02









programmer44

629




629












  • That's an XY problem and not really related to sessions or partial views. What are you trying to do? Cache query results? Or view output? MVC has its own mechanisms for output caching. EF has its own mechanisms too, or you can cache results in the Cache object or the Session.
    – Panagiotis Kanavos
    Nov 12 at 9:51










  • this might help
    – Hooman Bahreini
    Nov 12 at 9:55










  • Both cache and view ouput. I am already caching results in the Session.
    – programmer44
    Nov 12 at 9:56


















  • That's an XY problem and not really related to sessions or partial views. What are you trying to do? Cache query results? Or view output? MVC has its own mechanisms for output caching. EF has its own mechanisms too, or you can cache results in the Cache object or the Session.
    – Panagiotis Kanavos
    Nov 12 at 9:51










  • this might help
    – Hooman Bahreini
    Nov 12 at 9:55










  • Both cache and view ouput. I am already caching results in the Session.
    – programmer44
    Nov 12 at 9:56
















That's an XY problem and not really related to sessions or partial views. What are you trying to do? Cache query results? Or view output? MVC has its own mechanisms for output caching. EF has its own mechanisms too, or you can cache results in the Cache object or the Session.
– Panagiotis Kanavos
Nov 12 at 9:51




That's an XY problem and not really related to sessions or partial views. What are you trying to do? Cache query results? Or view output? MVC has its own mechanisms for output caching. EF has its own mechanisms too, or you can cache results in the Cache object or the Session.
– Panagiotis Kanavos
Nov 12 at 9:51












this might help
– Hooman Bahreini
Nov 12 at 9:55




this might help
– Hooman Bahreini
Nov 12 at 9:55












Both cache and view ouput. I am already caching results in the Session.
– programmer44
Nov 12 at 9:56




Both cache and view ouput. I am already caching results in the Session.
– programmer44
Nov 12 at 9:56












1 Answer
1






active

oldest

votes


















0














This answer explains that you cannot use Session with async calls, so you could implement your own cache, something like this:



public class MyController : Controller
{
private static object _lock = new object();
private static Dictionary<string, MyType> MyCache = new Dictionary<string, MyType>();

public ActionResult Action1(string triggerParameter)
{
// get data in action1
string userUniqueKey = GetUserId(); // <-- get a unique key for this user
var myObject = GetDataFromCache(triggerParameter, userUniqueKey);
}

public ActionResult Action2(string triggerParameter)
{
string userUniqueKey = GetUserId(); // <-- get a unique key for this user
var myObject = GetDataFromCache(triggerParameter, userUniqueKey);
}

// if data is already cached, get it from cache otherwise call GetData()
private MyType GetDataFromCache(string triggerParameter, string userKey)
{
lock (_lock)
{
if (MyCache.Contains(userKey))
{
return MyChache[userKey];
}
else
{
myTypeObject = GetData(triggerParameter);
MyCache.Add(userKey, myObjectType);
return myTypeObject;
}
}
}
}





share|improve this answer























  • Reading the data from the cache is what I am already doing(see SaveObjectToSession method). The problem is that not all the data is saved because the action that has to save the data in the cache is called async
    – programmer44
    Nov 12 at 9:50










  • The data is not saved in the cache, so even though I save the data in the cache in Action1, this data is not saved and it cannot be read from Action2.
    – programmer44
    Nov 12 at 9:51











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258789%2fasync-requests-in-asp-net-mvc-when-working-with-session-and-partial-views%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














This answer explains that you cannot use Session with async calls, so you could implement your own cache, something like this:



public class MyController : Controller
{
private static object _lock = new object();
private static Dictionary<string, MyType> MyCache = new Dictionary<string, MyType>();

public ActionResult Action1(string triggerParameter)
{
// get data in action1
string userUniqueKey = GetUserId(); // <-- get a unique key for this user
var myObject = GetDataFromCache(triggerParameter, userUniqueKey);
}

public ActionResult Action2(string triggerParameter)
{
string userUniqueKey = GetUserId(); // <-- get a unique key for this user
var myObject = GetDataFromCache(triggerParameter, userUniqueKey);
}

// if data is already cached, get it from cache otherwise call GetData()
private MyType GetDataFromCache(string triggerParameter, string userKey)
{
lock (_lock)
{
if (MyCache.Contains(userKey))
{
return MyChache[userKey];
}
else
{
myTypeObject = GetData(triggerParameter);
MyCache.Add(userKey, myObjectType);
return myTypeObject;
}
}
}
}





share|improve this answer























  • Reading the data from the cache is what I am already doing(see SaveObjectToSession method). The problem is that not all the data is saved because the action that has to save the data in the cache is called async
    – programmer44
    Nov 12 at 9:50










  • The data is not saved in the cache, so even though I save the data in the cache in Action1, this data is not saved and it cannot be read from Action2.
    – programmer44
    Nov 12 at 9:51
















0














This answer explains that you cannot use Session with async calls, so you could implement your own cache, something like this:



public class MyController : Controller
{
private static object _lock = new object();
private static Dictionary<string, MyType> MyCache = new Dictionary<string, MyType>();

public ActionResult Action1(string triggerParameter)
{
// get data in action1
string userUniqueKey = GetUserId(); // <-- get a unique key for this user
var myObject = GetDataFromCache(triggerParameter, userUniqueKey);
}

public ActionResult Action2(string triggerParameter)
{
string userUniqueKey = GetUserId(); // <-- get a unique key for this user
var myObject = GetDataFromCache(triggerParameter, userUniqueKey);
}

// if data is already cached, get it from cache otherwise call GetData()
private MyType GetDataFromCache(string triggerParameter, string userKey)
{
lock (_lock)
{
if (MyCache.Contains(userKey))
{
return MyChache[userKey];
}
else
{
myTypeObject = GetData(triggerParameter);
MyCache.Add(userKey, myObjectType);
return myTypeObject;
}
}
}
}





share|improve this answer























  • Reading the data from the cache is what I am already doing(see SaveObjectToSession method). The problem is that not all the data is saved because the action that has to save the data in the cache is called async
    – programmer44
    Nov 12 at 9:50










  • The data is not saved in the cache, so even though I save the data in the cache in Action1, this data is not saved and it cannot be read from Action2.
    – programmer44
    Nov 12 at 9:51














0












0








0






This answer explains that you cannot use Session with async calls, so you could implement your own cache, something like this:



public class MyController : Controller
{
private static object _lock = new object();
private static Dictionary<string, MyType> MyCache = new Dictionary<string, MyType>();

public ActionResult Action1(string triggerParameter)
{
// get data in action1
string userUniqueKey = GetUserId(); // <-- get a unique key for this user
var myObject = GetDataFromCache(triggerParameter, userUniqueKey);
}

public ActionResult Action2(string triggerParameter)
{
string userUniqueKey = GetUserId(); // <-- get a unique key for this user
var myObject = GetDataFromCache(triggerParameter, userUniqueKey);
}

// if data is already cached, get it from cache otherwise call GetData()
private MyType GetDataFromCache(string triggerParameter, string userKey)
{
lock (_lock)
{
if (MyCache.Contains(userKey))
{
return MyChache[userKey];
}
else
{
myTypeObject = GetData(triggerParameter);
MyCache.Add(userKey, myObjectType);
return myTypeObject;
}
}
}
}





share|improve this answer














This answer explains that you cannot use Session with async calls, so you could implement your own cache, something like this:



public class MyController : Controller
{
private static object _lock = new object();
private static Dictionary<string, MyType> MyCache = new Dictionary<string, MyType>();

public ActionResult Action1(string triggerParameter)
{
// get data in action1
string userUniqueKey = GetUserId(); // <-- get a unique key for this user
var myObject = GetDataFromCache(triggerParameter, userUniqueKey);
}

public ActionResult Action2(string triggerParameter)
{
string userUniqueKey = GetUserId(); // <-- get a unique key for this user
var myObject = GetDataFromCache(triggerParameter, userUniqueKey);
}

// if data is already cached, get it from cache otherwise call GetData()
private MyType GetDataFromCache(string triggerParameter, string userKey)
{
lock (_lock)
{
if (MyCache.Contains(userKey))
{
return MyChache[userKey];
}
else
{
myTypeObject = GetData(triggerParameter);
MyCache.Add(userKey, myObjectType);
return myTypeObject;
}
}
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 12 at 10:09

























answered Nov 12 at 9:40









Hooman Bahreini

3,1923730




3,1923730












  • Reading the data from the cache is what I am already doing(see SaveObjectToSession method). The problem is that not all the data is saved because the action that has to save the data in the cache is called async
    – programmer44
    Nov 12 at 9:50










  • The data is not saved in the cache, so even though I save the data in the cache in Action1, this data is not saved and it cannot be read from Action2.
    – programmer44
    Nov 12 at 9:51


















  • Reading the data from the cache is what I am already doing(see SaveObjectToSession method). The problem is that not all the data is saved because the action that has to save the data in the cache is called async
    – programmer44
    Nov 12 at 9:50










  • The data is not saved in the cache, so even though I save the data in the cache in Action1, this data is not saved and it cannot be read from Action2.
    – programmer44
    Nov 12 at 9:51
















Reading the data from the cache is what I am already doing(see SaveObjectToSession method). The problem is that not all the data is saved because the action that has to save the data in the cache is called async
– programmer44
Nov 12 at 9:50




Reading the data from the cache is what I am already doing(see SaveObjectToSession method). The problem is that not all the data is saved because the action that has to save the data in the cache is called async
– programmer44
Nov 12 at 9:50












The data is not saved in the cache, so even though I save the data in the cache in Action1, this data is not saved and it cannot be read from Action2.
– programmer44
Nov 12 at 9:51




The data is not saved in the cache, so even though I save the data in the cache in Action1, this data is not saved and it cannot be read from Action2.
– programmer44
Nov 12 at 9:51


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53258789%2fasync-requests-in-asp-net-mvc-when-working-with-session-and-partial-views%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues