【发布时间】:2018-03-04 11:26:07
【问题描述】:
public sealed class SessionContext
{
private ISession httpContext;
public SessionContext(ISession httpContext)
{
this.httpContext = httpContext;
}
public string UserType
{
get
{
return httpContext.GetString("_UserType");
}
set
{
httpContext.SetString("_UserType", value);
}
}
...... More properties .....
}
public class HomeController : Controller
{
private AppSettings _appSettings;
private SessionContext session = null;
private readonly IHttpContextAccessor _httpContextAccessor;
private ISession httpContext => _httpContextAccessor.HttpContext.Session;
//I don't like this constructor as it is getting initialize or every controller call.
public HomeController(IOptions<AppSettings> myAppSettings, IHttpContextAccessor httpContextAccessor)
{
_appSettings = myAppSettings.Value;
_httpContextAccessor = httpContextAccessor;
appSettings = new AppSettings(_appSettings); //Should initialize only once.
session = new SessionContext(httpContext);
}
}
我有关于...的问题
- 如何使用 Asp.Net Core 2.0 在 MVC 6 中初始化和使用自定义/支持类 当我初始化这些类时,它们会被初始化或每次控制器调用。这是非常多余的。
-
我的 SessionContext 类每次都重新初始化。所以当我从另一个控制器调用这个类时,我失去了这些值。
我尝试了这种方法,但用处不大。 services.AddSingleton();
【问题讨论】:
-
终于...解决了问题。感谢博客强类型配置设置在 ASP.NET Core Part II .... 由 Khalid Abuhakmeh 撰写
标签: class asp.net-core-mvc helper