How to get asp.net class value for session in view
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 to share