I am trying to use session in asp.net mvc 6 from calling class inside of Iactionresult.
I have class as "SessionTool as below"
SessionTool class:
public class SessionTool
{
private readonly IHttpContextAccessor HttpContextAccessor;
public SessionTool(IHttpContextAccessor httpContextAccessor)
{
HttpContextAccessor = httpContextAccessor;
}
public void SetSession(string key, string value)
{
HttpContextAccessor.HttpContext.Session.SetString(key, value);
}
public string GetSession(string key)
{
return HttpContextAccessor.HttpContext.Session.GetString(key);
}
}
I have HomeController as below.
public class HomeController : Controller
{
[HttpGet]
public IActionResult Login()
{
SessionTool sessionTool = new SessionTool(IHttpContextAccessor);
}
}
Question:
If i try to call SessionTools class as below IHttpContextAccessor displays warning as IHttpContextAccessor is a type , which is not valid in the given context.
SessionTool sessionTool = new SessionTool(IHttpContextAccessor);
I am new in asp.net mvc 6 and i am not sure how can i set new instance of IHttpContextAccessor to session tool than use session classes ?
Any help will be appreciated. Thanks