Error:
AspNet Core 2 No service for type 'Microsoft.AspNetCore.Http.HttpContext'
im using https://docs.microsoft.com/en-us/aspnet/core/fundamentals/app-state?view=aspnetcore-2.1&tabs=aspnetcore2x as guide, but seems to be not enough.
in Startup.cs
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddDistributedMemoryCache();
- services.AddMvc().AddSessionStateTempDataProvider();
- services.AddSession(options =>
- {
-
- options.IdleTimeout = TimeSpan.FromMinutes(1);
- options.Cookie.HttpOnly = true;
- });
- }
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- app.UseBrowserLink();
- }
- else
- {
- app.UseExceptionHandler("/Home/Error");
- }
- app.UseStaticFiles();
- app.UseSession();
- app.UseMvc(routes =>
- {
- routes.MapRoute(
- name: "default",
- template: "{controller=Home}/{action=Index}/{id?}");
- routes.MapSpaFallbackRoute(
- name: "spa-fallback",
- defaults: new { controller = "Home", action = "Index" });
- });
- }
in home controller:
- using Microsoft.AspNetCore.Http;
- HttpContext.Session.SetString("TestName", user.UserName);
- HttpContext.Session.SetString("TestVal", user.testVal.ToString());
to this point it seems to be working, im able to navigate through different pages and i can use/check sessions in controller side.
But the problem is when im trying to use sessions in any view
View side:
- @using Microsoft.AspNetCore.Http
- @inject HttpContext HttpContext
- @if (@HttpContext.Session.GetString("testVal").Equals("True"))
- {
- <td><input type="checkbox"></td>
- }
- else
- {
- <td><input type="checkbox" readonly></td>
- }
when i try to get or set sessions cshtml/view side i get the error
can you please help?