PDA

View Full Version : معادل HttpContextBase در mvc.core



Iran58
پنج شنبه 02 مرداد 1399, 12:13 عصر
سلام
من کد زیر را در mvc.net نوشته ام
اما وقتی می خواهم mvc.core بنویسم
HttpContextBase نمی توانم فراخوانی کنم باید چه تغییراتی در کدم ایجاد کنم

public static class CookieHelper
{
public static void AddCookie(this HttpContextBase httpContextBase, string cookieName, string value, short time)
{
httpContextBase.AddCookie(cookieName, value, DateTime.Now.AddMinutes(time));
}


public static void RemoveCookie(this HttpContextBase httpContextBase, string cookieName)
{
var cookie = new HttpCookie(cookieName)
{
Expires = DateTime.Now.AddYears(-100)
};
httpContextBase.Response.Cookies.Set(cookie);
}


public static void UpdateCookie(this HttpContextBase httpContextBase, string cookieName, string value, bool httpOnly = false)
{
var cookie = new HttpCookie(cookieName)
{
Value = httpContextBase.Server.UrlEncode(value),
HttpOnly = httpOnly
};
httpContextBase.Response.Cookies.Set(cookie);
}
public static void AddCookie(this HttpContextBase httpContextBase, string cookieName, string value, DateTime expires, bool httpOnly = false)
{
var cookie = new HttpCookie(cookieName)
{
Expires = expires,
Value = httpContextBase.Server.UrlEncode(value),
HttpOnly = httpOnly
};
httpContextBase.Response.Cookies.Add(cookie);
}


public static string GetCookieValue(this HttpContextBase httpContext, string cookieName)
{
var cookie = httpContext.Request.Cookies[cookieName];
if (cookie == null)
return string.Empty;
return httpContext.Server.UrlDecode(cookie.Value);
}


public static void UpdateCookieUserOderBySession(HttpContextBase httpContext, int time)
{
var cookieValue = httpContext.GetCookieValue(WordsUseful.CookieUser) ;
httpContext.AddCookie(WordsUseful.CookieUser, cookieValue, DateTime.Now.AddMinutes(time));
}
}

nunegandom
سه شنبه 07 مرداد 1399, 15:15 عصر
سلام
IHttpContextAccessor (https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.ihttpcontextaccessor?vie w=aspnetcore-3.1)