zoukankan      html  css  js  c++  java
  • .NET Core中使用Session步骤

    .NET Core中使用Session步骤如下:

    1、安装Microsoft.AspNetCore.Session    NuGet包

    2、修改Startup.cs 添加相关服务,services.AddSession()和app.UseSession()

    // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddDbContext<ApplicationDbContext>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));
    
                services.AddIdentity<ApplicationUser, IdentityRole>()
                    .AddEntityFrameworkStores<ApplicationDbContext>()
                    .AddDefaultTokenProviders();
    
                // Add application services.
                services.AddTransient<IEmailSender, EmailSender>();
    
                //Add session
                services.AddSession();
    
                services.AddMvc();
            }
     // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
    
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                    app.UseBrowserLink();
                    app.UseDatabaseErrorPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                }
    
                app.UseStaticFiles();
    
                app.UseAuthentication();
              
                app.UseSession();
    
                app.UseMvc(routes =>
                {
                    //Default route
                    routes.MapRoute(
                        name: "default",
                        template: "{controller=Home}/{action=Index}/{id?}");
                });
            }

    3、Session 用法

    引用

    using Microsoft.AspNetCore.Http;

    Session写入

    HttpContext.Session.SetString("key", "value");

    Session读取

    HttpContext.Session.GetString("key");
  • 相关阅读:
    课程总结第十一周
    用户场景分析
    团队冲刺10
    课程总结第十周
    团队冲刺09
    梦断代码阅读笔记03
    转发和重定向的区别
    request
    servletConfig
    servlet
  • 原文地址:https://www.cnblogs.com/liushunli/p/10346403.html
Copyright © 2011-2022 走看看