asp.net core中Microsoft.AspNet.Session的使用


1、通過nuget/修改project.json引用  

2、引用存儲session的媒介,如內存、數據庫、redis等

    "Microsoft.AspNetCore.Session": "1.1.0-*",

    "Microsoft.Extensions.Caching.Memory": "1.1.0-*",

    "Microsoft.Extensions.Caching.Redis": "1.1.0-*",

    "Microsoft.Extensions.Caching.SqlServer": "1.1.0-*",

 

3、修改startup.cs

public void ConfigureServices(IServiceCollection services)
        {
            services.AddDistributedMemoryCache();
            services.AddSession();
            services.AddMvc();
        }
 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            app.UseSession();

4、使用

Context.Session.GetString("UserName")

 5、參考

示例:https://github.com/aspnet/Session/tree/dev/samples/SessionSample

倉庫:https://github.com/aspnet/Session


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM