How to get asp.net class value for session in view

I am trying to get a session like this

@HttpContext.Session.GetString("some");

      

But i get

*

Object reference is required for non-static field ......

*

Anyone with ideas?

+3


source to share


1 answer


You need to inject the implementation IHttpContextAccessor

into the views and use it.

@inject Microsoft.AspNetCore.Http.IHttpContextAccessor HttpContextAccessor

      

Now you can access the property HttpContext

and thenSession

<p>
    @HttpContextAccessor.HttpContext.Session.GetInt32("MySessionKey")
</p>

      

Assuming you have all the necessary settings to enable session in class startup

.



In your method ConfigureServices

services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

      

and IApplicationBuilder.UseSession

method call Configure

.

app.UseSession();

      

+6


source







All Articles