TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
David Alejandro García García
NA
14
720
AspNet Core 2 No service for HttpContext sessions issue
May 10 2018 10:48 AM
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 =>
{
// Set a short timeout for easy testing.
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?
Reply
Answers (
1
)
Logoff When we change Url parameter intger to string
Sending msgs to Set of whatsapp numbers at specified time.