Mvc
Mvc copied to clipboard
Read session variable in cshtml results in error
Packages.json
"Microsoft.AspNet.Http": "1.0.0-beta8",
"Microsoft.AspNet.Session": "1.0.0-beta8",
View _Layout.cshtml
@using Microsoft.AspNet.Http;
and
@{
int? myInt= Context.Session.GetInt32("myInt");
}
Run
InvalidOperationException: Session has not been configured for this application or request.
Microsoft.AspNet.Http.Internal.DefaultHttpContext.get_Session()
Asp.ASPV__Views_Shared__Layout_cshtml.<ExecuteAsync>d__26.MoveNext() in _Layout.cshtml
int? myInt= Context.Session.GetInt32("myInt");
--- End of stack trace from previous location where exception was thrown ---
Changing it to a set
Context.Session.SetInt32("myInt",1);
gives the same error
You have to add the session feature to your application in the Startup class:
public void ConfigureServices(IServiceCollection services)
{
// ...
services.AddSession();
// ...
}
public void Configure(IApplicationBuilder app)
{
// ...
app.UseSession();
// ...
}
Thanks forgot I commented it out when troubleshooting.