我有一个场景,我使用 Session 将对象存储在一个控制器操作中,并尝试在另一个控制器操作中检索它。这两个动作都是从同一个视图触发的,并且驻留在同一个控制器上。不幸的是,我无法在第二个控制器中检索会话变量。会话 ID 保持不变,我确保在第一个操作中将对象写入会话。但是,当视图在第一个操作中返回时,会话数据会消失。
这是我的控制器代码流-
public PartialviewResult DoSearch(string paramCustId)
{
//invoking a method to perform a search task. I am also passing the controller session as a parameter
//this function is called in a separate thread and the main thread does not wait for it to complete before returning the view
multiSearch(paramCustId, Session);
}
return PartialView("_partialView1");
public void multiSearch(string searchParam, HttpSessionStateBase controllerSession)
{
//code to retrieve response from backend into the variable tempSearchSet
controllerSession["searchResult"] = tempSearchSet;
//verified that tempSearchSet is stored in Session under the key "searchResult" and Session.Count is 1.
}
//Another controller action that is triggered from the same view after a certain delay to fetch the data in session
public PartialViewResult PollSearchResults()
{
var tempSearchResult = Session["searchResult"] as List<SearchResultSet>;
//This is where i do not see data in the session. I have verified that the multiSearch method is complete and has updated the data in the session.
//here Session.SessionID is the same as above, but Session.Count is 0
}
有没有不同的方法来处理 mvc 中的 Session 或者我在这里遗漏了一些基本的东西?此外,是否有更好的方法来管理这种缓存方案?