Snippet

I was using TempView in ASP.NET Core and received a run-time error message, telling me that Session was not enabled.

Here is how I got it to work:

Add two entries to dependencies in your config.json file:

"Microsoft.AspNet.Session": "1.0.0-*",
"Microsoft.Extensions.Caching.Memory": "1.0.0-*"

Then add the following code to your ConfigureServices method in Startup.cs:

services.AddCaching();
services.AddSession(options => { 
        options.IdleTimeout = TimeSpan.FromMinutes(30); 
        options.CookieName = ".YourApplication";
    });

This is also where you set your Session timeout and Session cookie name.

Finally, add the following line to your Configure method in Startup.cs:

app.UseSession();

The order is important in the pipeline, so be sure this is placed before your app.UseMvc() call.