eva*_*que 13 c# bytearray session-state type-conversion asp.net-core
我正在编写ASP.NET 5 MVC 6(核心)应用程序.现在我来到了一个需要在session-cache(ISession)中存储(设置和获取)对象的点.
你可能知道,Set-method ISession取a byte-array和Get-method返回一个.
在非核心应用程序中,我会使用它BinaryFormatter来转换我的对象.但是我怎么能在核心应用程序中做到这一点?
Hen*_*ema 27
我将对象序列化为JSON并使用扩展方法ISession将它们保存为string.
// Save
var key = "my-key";
var str = JsonConvert.SerializeObject(obj);
context.Session.SetString(key, str);
// Retrieve
var str = context.Session.GetString(key);
var obj = JsonConvert.DeserializeObject<MyType>(str);
Run Code Online (Sandbox Code Playgroud)
扩展方法on ISession在Microsoft.AspNet(Core).Http命名空间中定义.
| 归档时间: |
|
| 查看次数: |
11562 次 |
| 最近记录: |