Accessing the Session in ASP.NET Core 1

The of the biggest changes at least for me to tackle is going from having everything available to me all the time to have to Dependency Inject everything I need (and then knowing when and where I can do that to get the pieces I want where I want them).

I'm not going to tackle whether you should or shouldn't use this code or whether it's the right thing but I will show you how to get the Session wherever you want it.

First things first.  ASP.NET Core 1 is pluggable so you literally have to tell it all of the components you want.  This is some extra overhead and learning from a coding perspective but it gives you the ability to create the smallest pipeline possible to do what you need your site to do (and I assume after you do it 15 times this part will become trivial).


This is where we configure our services.  You should see a method called ConfigureServices that is adding things, you will need an entry in this method to add the session's in (notice session comes after AddMvc here.

public void ConfigureServices(IServiceCollection services)

Now, you should also have a Configure method.  You will also need to add the session here but it is **important **that it comes before the varization of UseMvc in this method (or it won't work, at least not in RC1).

public void Configure(IApplicationBuilder app, IHostingEnvironment env, IOptions appSettingsAccessor, UserService userService)

    if (env.IsDevelopment())


Those are the plumbling pieces above that you need to tell your site to make the Session available.  Now, how to use it from the various places you may want it.


These are easy.  The HttpContext that contains the session is are already available in the controller.  This means you can simply HttpContext.Session to access it.

HttpContext.Session.SetString("TestText", "Hi, this is a test from your controller at " + DateTime.Now.ToString());


There are two ways (I can see)  that you can get the session in a View.  The first way is to pass it from the controller to the View via a ViewModel (or rather, pass just the values you want to send through to the View).  Most people will probably say this is the preferred way and I won't argue. You can also directly inject the Session via the HttpContextAccessor into the view if it were your model (e.g. you're only showing something from the session on the page in question).

In order to do this, you'll need to add a using and an inject to the top of the view.

    @using Microsoft.AspNet.Http
    @inject Microsoft.AspNet.Http.IHttpContextAccessor HttpContextAccessor

To use the Session here you would then do something like this (we'll read out the variable we set previously).

    This is from the session - @HttpContextAccessor.HttpContext.Session.GetString("TestText")

Now, if you need it in a ViewModel or a Model for some reason what you will probably want to do is pass it in from the controller.  The dependency injection works for instances of classes that the ASP.NET creates for you (like the controller).  In the controller, you can put those things you want in the constructor and the framework handles injected them to you.  In your own classes that you create, this doesn't work (it would expect you to provide them).  Typically a Model or a ViewModel is marshalling data between concerns so it seems appropriate that you can just pass off the specific data you need from the controller.

This is being written in the RC1 time frame so my caveat is that this is all subject to change.

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.