[英]ASPNET Core replacement for [Session] =
We are migrating from ASPNET MVC5 to ASPNET Core meaning we need to refactor some code.我们正在从 ASPNET MVC5 迁移到 ASPNET Core,这意味着我们需要重构一些代码。
We were using Session = model to store the model in the session, then retrieving it from another Controller. We were using Session = model to store the model in the session, then retrieving it from another Controller.
We understand this option has been discontinued in Core.我们了解此选项已在 Core 中停止使用。
We have attempted to use: HttpContext.Session.SetString("Data", JsonConvert.SerializeObject(model));
我们尝试使用: HttpContext.Session.SetString("Data", JsonConvert.SerializeObject(model));
However, when Deserialising by using:但是,当使用反序列化时:
var json = HttpContext.Session.GetString("Data");
var model = JsonConvert.DeserializeObject<SearchListViewModel>(json);
The resulting model does not come back the same - it is one long string rather than a structured list (which is was before Serialising).生成的 model 不会返回相同的结果 - 它是一个长字符串而不是结构化列表(在序列化之前)。
Is there a better way to achieve passing a model from one controller to another?有没有更好的方法来实现将 model 从一个 controller 传递到另一个?
you can still use session if you need.如果需要,您仍然可以使用 session。 You just need to config it in startup你只需要在启动时配置它
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddControllersWithViews();
services.AddSession();
....another your code
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseSession();
... another your code
}
Assuming that you have configured your Session
in your Startup.cs
, an example of how you can do this:假设您已经在Startup.cs
中配置了Session
,以下示例说明了如何执行此操作:
Create a model class of your object type (whatever you want to store).创建 object 类型的 model class (无论您想存储什么)。 I am giving a simple example:我举一个简单的例子:
public class SearchListViewModel
{
public int SearchID{ get; set; }
public string SearchName{ get; set; }
//so on
}
Then create a SessionExtension
helper to set and retrieve your complex object as JSON:然后创建一个SessionExtension
助手来设置和检索复杂的 object 为 JSON:
public static class SessionExtensions
{
public static void SetObjectAsJson(this ISession session, string key, object value)
{
session.SetString(key, JsonConvert.SerializeObject(value));
}
public static T GetObjectFromJson<T>(this ISession session, string key)
{
var value = session.GetString(key);
return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
}
}
Then finally set the complex object in your session as:然后最后将 session 中的复杂 object 设置为:
var search= new SearchListViewModel();
search.SearchID= 1;
search.SearchName= "Test";
HttpContext.Session.SetObjectAsJson("SearchListViewModel", search);
To retrieve your complex object in your session:要在 session 中检索复杂的 object:
var searhDetails = HttpContext.Session.GetObjectFromJson<SearchListViewModel>("SearchListViewModel");
int searchID= SearchListViewModel.SearchID;
string searchName= SearchListViewModel.SearchName;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.