這篇文章將為大家詳細(xì)講解有關(guān)解析Asp.net使用Session的方法,小編覺得挺實(shí)用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。

Session 是保存用戶和 Web 應(yīng)用的會話狀態(tài)的一種方法,ASP.NET Core 提供了一個用于管理會話狀態(tài)的中間件,本篇文章主要介紹了Asp.net Core中使用Session ,有興趣的可以了解一下、
前言
2017年就這么悄無聲息的開始了,2017年對我來說又是特別重要的一年。
元旦放假在家寫了個Asp.net Core驗(yàn)證碼登錄, 做demo的過程中遇到兩個小問題,第一是在Asp.net Core中引用dll,以往我們引用DLL都是直接引用,在Core里這樣是不行的,必須基于NuGet添加,或者基于project.json添加,然后保存VS會啟動還原類庫。
第二就是使用Session的問題,Core里使用Session需要添加Session類庫。
添加Session
在你的項(xiàng)目上基于NuGet添加:Microsoft.AspNetCore.Session。
修改startup.cs
在startup.cs找到方法ConfigureServices(IServiceCollection services) 注入Session(這個地方是Asp.net Core pipeline):services.AddSession();
接下來我們要告訴Asp.net Core使用內(nèi)存存儲Session數(shù)據(jù),在Configure(IApplicationBuilder app,...)中添加代碼:app.UserSession();
Session
1、在MVC Controller里使用HttpContext.Session
using Microsoft.AspNetCore.Http;
public class HomeController:Controller
{
public IActionResult Index()
{
HttpContext.Session.SetString("code","123456");
return View();
}
public IActionResult About()
{
ViewBag.Code=HttpContext.Session.GetString("code");
return View();
}
}2、如果不是在Controller里,你可以注入IHttpContextAccessor
public class SomeOtherClass
{
private readonly IHttpContextAccessor _httpContextAccessor;
private ISession _session=> _httpContextAccessor.HttpContext.Session;
public SomeOtherClass(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor=httpContextAccessor;
}
public void Set()
{
_session.SetString("code","123456");
}
public void Get()
{
string code = _session.GetString("code");
}
}存儲復(fù)雜對象
存儲對象時把對象序列化成一個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);
}
}var myComplexObject = new MyClass();
HttpContext.Session.SetObjectAsJson("Test", myComplexObject);
var myComplexObject = HttpContext.Session.GetObjectFromJson<MyClass>("Test");使用SQL Server或Redis存儲
1、SQL Server
添加引用 "Microsoft.Extensions.Caching.SqlServer": "1.0.0"
注入:
// Microsoft SQL Server implementation of IDistributedCache.
// Note that this would require setting up the session state database.
services.AddSqlServerCache(o =>
{
o.ConnectionString = "Server=.;Database=ASPNET5SessionState;Trusted_Connection=True;";
o.SchemaName = "dbo";
o.TableName = "Sessions";
});2、Redis
添加引用 "Microsoft.Extensions.Caching.Redis": "1.0.0"
注入:
// Redis implementation of IDistributedCache. // This will override any previously registered IDistributedCache service. services.AddSingleton<IDistributedCache, RedisCache>();
關(guān)于解析Asp.net使用Session的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
本文標(biāo)題:解析Asp.net使用Session的方法-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://www.chinadenli.net/article40/idiho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、營銷型網(wǎng)站建設(shè)、Google、網(wǎng)站建設(shè)、企業(yè)網(wǎng)站制作、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容