C# 如何在静态类中使用IHttpContextAccessor设置Cookie
我试图在静态类中创建一个泛型的C# 如何在静态类中使用IHttpContextAccessor设置Cookie,c#,asp.net-core,cookies,.net-core,asp.net-core-mvc,C#,Asp.net Core,Cookies,.net Core,Asp.net Core Mvc,我试图在静态类中创建一个泛型的addReplaceCookie方法。这个方法看起来像这样 public static void addReplaceCookie(string cookieName, string cookieValue) { if ((HttpContext.Current.Request.Cookies(cookieName) == null)) { // add cookie HttpCookie s = new HttpC
addReplaceCookie
方法。这个方法看起来像这样
public static void addReplaceCookie(string cookieName, string cookieValue)
{
if ((HttpContext.Current.Request.Cookies(cookieName) == null))
{
// add cookie
HttpCookie s = new HttpCookie(cookieName);
s.Value = cookieValue;
s.Expires = DateTime.Now.AddDays(7);
HttpContext.Current.Response.Cookies.Add(s);
}
else {
// ensure cookie value is correct
HttpCookie existingSchoolCookie = HttpContext.Current.Request.Cookies(cookieName);
existingSchoolCookie.Expires = DateTime.Now.AddDays(7);
existingSchoolCookie.Value = cookieValue;
HttpContext.Current.Response.Cookies.Set(existingSchoolCookie);
}
}
我知道,为了在asp.net核心中获得HttpContext
,您必须使用IHttpContextAccessor
。但我不能将其注入到静态类中
有没有其他方法可以访问它
我使用的是rc1 final。如果可能,不要使用
静态类。但如果必须使用,将IHttpContextAccessor
作为参数发送可能是一种解决方案
public static void addReplaceCookie(string cookieName, string cookieValue, IHttpContextAccessor accessor)
{
//your code
}
public class CallerClass
{
private readonly IHttpContextAccessor _accessor;
public CallerClass(IHttpContextAccessor accessor)
{
_accessor = accessor;
addReplaceCookie(cookieName, cookieValue, accessor);
}
}
虽然我建议远离这样的静态类场景,但仍然可以实现您的要求
在Startup.ConfigureServices
方法中,您可以调用services.BuildServiceProvider()
以获取IServiceProvider
以解析所需的类型。这是一个有点黑客,但它的工作
假设一个静态类如
public class MyStaticHelperClass {
private static IHttpContextAccessor httpContextAccessor;
public static void SetHttpContextAccessor(IHttpContextAccessor accessor) {
httpContextAccessor = accessor;
}
public static void addReplaceCookie(string cookieName, string cookieValue) {
var HttpContext = httpContextAccessor.HttpContext;
if (HttpContext.Request.Cookies(cookieName) == null) {
// add cookie
HttpCookie s = new HttpCookie(cookieName);
s.Value = cookieValue;
s.Expires = DateTime.Now.AddDays(7);
HttpContext.Response.Cookies.Add(s);
} else {
// ensure cookie value is correct
HttpCookie existingSchoolCookie = HttpContext.Request.Cookies(cookieName);
existingSchoolCookie.Expires = DateTime.Now.AddDays(7);
existingSchoolCookie.Value = cookieValue;
HttpContext.Response.Cookies.Set(existingSchoolCookie);
}
}
}
您将在启动期间配置类
public IServiceProvider ConfigureServices(IServiceCollection service) {
services.AddTransient<IMyService, MyService>();
services.AddMvc();
//this would have been done by the framework any way after this method call;
//in this case you call the BuildServiceProvider manually to be able to use it
var serviceProvider = services.BuildServiceProvider();
//here is where you set you accessor
var accessor = serviceProvider.GetService<IHttpContextAccessor>()
MyStaticHelperClass.SetHttpContextAccessor(accessor);
return serviceProvider;
}
…然后可以在服务集合中注册
public void ConfigureServices(IServiceCollection service) {
services.AddTransient<ICookieService, CookieService>();
services.AddMvc();
}
这是我在应用程序中管理会话cookie的方法。在Startup.ConfigureServices中:
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
services.AddSingleton();
稍后启动。通过DI IServiceProvider配置add,并使用它提取IHttpContextAccessor,如下所示:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider svp)
{
IHttpContextAccessor accessor = svp.GetService<IHttpContextAccessor>();
MyRepository.SetHttpContextAccessor(accessor);
public void配置(IApplicationBuilder应用程序、IHostingEnvironment环境、IServiceProvider svp)
{
IHttpContextAccessor accessor=svp.GetService();
SetHttpContextAccessor(accessor);
因为它是一个静态类,所以你可以创建一个初始化方法,在设置服务集合的过程中接收接口并传递它。太棒了,我想我已经开始了解它了。你知道吗,有没有办法替换asp.net core中的cookie,或者append会自动替换它?你说的append是什么意思?我不确定我在使用它站起来。我通常使用Response.Cookies.Set(cookie)
,如果这是你的意思。cookie有键和值的地方。是的,出于某种原因Response.Cookies.Set为我抛出了一个错误。我必须使用Response.Cookies.Append(cookieName、cookieValue、CookieOption);但是那很好。谢谢你的帮助!你能描述一下为什么不推荐静态类用于此场景,以及你推荐的场景是什么吗?
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
public void Configure(IApplicationBuilder app, IHostingEnvironment env, IServiceProvider svp)
{
IHttpContextAccessor accessor = svp.GetService<IHttpContextAccessor>();
MyRepository.SetHttpContextAccessor(accessor);