【问题标题】:Accessing Cookies from static class in ASP.net Core从 ASP.net Core 中的静态类访问 Cookie
【发布时间】:2021-08-04 15:22:40
【问题描述】:

我正在尝试从 Asp.net Core 中的静态类访问 Cookie,到目前为止,我在 Google 上找不到任何有用的主题。有什么建议吗?

我已经在 asp.net 中做到了:

public static string GetString(string Key)
        {
            var format = System.Web.HttpContext.Current.Request.Cookies["Language"];
            if (format == null)
                format = new HttpCookie("Language") { Value = "Fa" };
            ResourceManager rm = new ResourceManager("Ippv.Virtual.Application.Properties." + format.Value,
                Assembly.GetExecutingAssembly());
            return rm.GetString(Key);
        }

如何在 asp.net core 中编写等效的代码?

【问题讨论】:

  • 显示一些相关代码会有所帮助。为什么不能将cookie值作为参数传递给静态方法?

标签: c# asp.net-core cookies


【解决方案1】:

在 ASP.NET 中,我们可以使用 httpcontext.current 访问 cookie,但在 ASP.NET Core 中,没有 htttpcontext.current。在 ASP.NET Core 中,一切都是解耦和模块化的。Httpcontext 可以从 Request 对象和“Microsoft.AspNetCore.Http”命名空间下的 IHttpContextAccessor 接口访问,并且在应用程序的任何地方都可用。参考以下代码:

 public class TestController : Controller
    {
        private readonly IHttpContextAccessor _httpContextAccessor;

        public TestController(IHttpContextAccessor httpContextAccessor)
        {
            this._httpContextAccessor = httpContextAccessor;
        }
        public  IActionResult Index()
        {
 //read cookie from IHttpContextAccessor  
            string cookieValueFromContext = _httpContextAccessor.HttpContext.Request.Cookies["key"];
            //read cookie from Request object  
            string cookieValueFromReq = Request.Cookies["Key"];
            //set the key value in Cookie  
            Set("kay", "Hello from cookie", 10);
            //Delete the cookie object  
            Remove("Key");
            return View();
        }
        /// <summary>  
        /// Get the cookie  
        /// </summary>  
        /// <param name="key">Key </param>  
        /// <returns>string value</returns>  
        public string Get(string key)
        {
            return Request.Cookies[key];
        }
        /// <summary>  
        /// set the cookie  
        /// </summary>  
        /// <param name="key">key (unique indentifier)</param>  
        /// <param name="value">value to store in cookie object</param>  
        /// <param name="expireTime">expiration time</param>  
        public void Set(string key, string value, int? expireTime)
        {
            CookieOptions option = new CookieOptions();
            if (expireTime.HasValue)
                option.Expires = DateTime.Now.AddMinutes(expireTime.Value);
            else
                option.Expires = DateTime.Now.AddMilliseconds(10);
            Response.Cookies.Append(key, value, option);
        }
        /// <summary>  
        /// Delete the key  
        /// </summary>  
        /// <param name="key">Key</param>  
        public void Remove(string key)
        {
            Response.Cookies.Delete(key);
        }
   

【讨论】:

  • 感谢您的帮助
猜你喜欢
  • 2018-02-03
  • 2020-02-02
  • 1970-01-01
  • 2011-07-31
  • 1970-01-01
  • 2016-12-21
  • 1970-01-01
  • 2021-03-19
  • 1970-01-01
相关资源
最近更新 更多