成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

Session怎么在Asp.netCore項(xiàng)目中使用-創(chuàng)新互聯(lián)

這篇文章給大家介紹Session怎么在Asp.net Core項(xiàng)目中使用,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。

網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)建站!專注于網(wǎng)頁(yè)設(shè)計(jì)、網(wǎng)站建設(shè)、微信開(kāi)發(fā)、小程序開(kāi)發(fā)、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項(xiàng)目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了安義免費(fèi)建站歡迎大家使用!

添加Session


在你的項(xiàng)目上基于NuGet添加:Microsoft.AspNetCore.Session。

修改startup.cs

在startup.cs找到方法ConfigureServices(IServiceCollection services) 注入Session(這個(gè)地方是Asp.net Core pipeline):services.AddSession();

接下來(lái)我們要告訴Asp.net Core使用內(nèi)存存儲(chǔ)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");
   }
}

存儲(chǔ)復(fù)雜對(duì)象

存儲(chǔ)對(duì)象時(shí)把對(duì)象序列化成一個(gè)json字符串存儲(chǔ)。

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存儲(chǔ)

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)于Session怎么在Asp.net Core項(xiàng)目中使用就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

當(dāng)前標(biāo)題:Session怎么在Asp.netCore項(xiàng)目中使用-創(chuàng)新互聯(lián)
新聞來(lái)源:http://jinyejixie.com/article26/dehgjg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、云服務(wù)器網(wǎng)站維護(hù)、營(yíng)銷型網(wǎng)站建設(shè)微信公眾號(hào)、用戶體驗(yàn)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

微信小程序開(kāi)發(fā)
青川县| 秦皇岛市| 江孜县| 柳林县| 保靖县| 和静县| 龙岩市| 长春市| 定襄县| 视频| 彭水| 礼泉县| 麻阳| 鄂州市| 启东市| 伽师县| 大新县| 盘锦市| SHOW| 马鞍山市| 微博| 应城市| 时尚| 越西县| 金溪县| 台南县| 亳州市| 武汉市| 小金县| 麻栗坡县| 巴彦淖尔市| 孟村| 兰西县| 莱西市| 泾川县| 四川省| 新源县| 泸定县| 邵东县| 章丘市| 房山区|