分类:
.NET Core
//1.自定义一个类Mysessionextensions将以下代码复制进去 public static class Mysessionextensions {//自定义存储session值 public static void Setobj<T>(this ISession session,string Key,T values) { string jsonstr = JsonConvert.SerializeObject(values); byte[] byteArray = Encoding.Default.GetBytes(jsonstr); session.Set(Key, byteArray); } //自定义取值 public static T Getobj<T>(this ISession session, string Key) { byte[] byteArray; bool isvalue = session.TryGetValue(Key, out byteArray); if (isvalue) { string str = Encoding.Default.GetString(byteArray); T val = JsonConvert.DeserializeObject<T>(str); return val; } else { return default(T); } } } //使用自定义session//先创建一个类将其复制 UserInfore UserInfore = new UserInfore(); UserInfore.Id = 1; UserInfore.username = "aa"; //自定义存值 HttpContext.Session.Setobj("userinfore",UserInfore); //自定义session拿值 UserInfore userInfore = HttpContext.Session.Getobj<UserInfore>("userinfore");
评价