Aspnetcore: HttpContext.Session.SetString

Created on 17 Jun 2016  ·  3Comments  ·  Source: dotnet/aspnetcore

AspNet core rc2
package: Microsoft.AspNetCore.Session

    HttpContext.Session.SetString(key,value)   error.

I have add code in the startUp.cs.

        public void ConfigureServices(IServiceCollection services)
        {
            // Add framework services
            services.AddApplicationInsightsTelemetry(Configuration);
            services.AddAuthorization();
            services.AddSession(options =>
            {
                options.IdleTimeout = TimeSpan.FromDays(7);
                options.CookieName = ".FileSystem";
            });
            services.AddMvc();
        }
         public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            #region

            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();

            app.UseApplicationInsightsRequestTelemetry();

            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseBrowserLink();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }

            app.UseApplicationInsightsExceptionTelemetry();

            app.UseStaticFiles();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });

            #endregion

            app.UseSession();
        }
    }

** Not working. HttpContext.session.setString()

error: Session has not been configured for this application or request.

I have read some blogs: asp.net core rc1.
The blogs shows that i should import the package: aspnet.session.
but when i import this package, the package can not support app.UseSession() function.

Help! how can i use session in aspNet core RC2

Most helpful comment

Middlewares get added to the pipeline in the order in which they appear in your code. In this case, UseSession appears after UseMvc; consequently it isn't set up until after Mvc has executed. Add it at some point before your call to UseMvc. https://docs.asp.net/en/latest/fundamentals/middleware.html#creating-a-middleware-pipeline-with-iapplicationbuilder has pretty useful information as to how this works.

All 3 comments

Middlewares get added to the pipeline in the order in which they appear in your code. In this case, UseSession appears after UseMvc; consequently it isn't set up until after Mvc has executed. Add it at some point before your call to UseMvc. https://docs.asp.net/en/latest/fundamentals/middleware.html#creating-a-middleware-pipeline-with-iapplicationbuilder has pretty useful information as to how this works.

@pranavkm Thanks very much.

useless

Was this page helpful?
0 / 5 - 0 ratings

Related issues

markrendle picture markrendle  ·  3Comments

ermithun picture ermithun  ·  3Comments

dotNETSanta picture dotNETSanta  ·  3Comments

UweKeim picture UweKeim  ·  3Comments

Pixel-Lord picture Pixel-Lord  ·  3Comments