如何在公共类中使用HttpContext
本文关键字:HttpContext | 更新日期: 2023-09-27 17:59:11
如何在公共类中使用HttpContext。我在一个类中有关于Cookie的所有方法,没有这个:
public HttpCookie SetAndGetHttpCookies()
{
HttpCookie cookie = HttpContext.Request.Cookies.Get("MyCookieGitApplication");
CookieHelper cookieHelper = new CookieHelper();
if (cookie == null)
{
cookie = cookieHelper.SetCookie();
}
return cookie;
}
知道我如何在公共类中使用HttpContext.Request吗?也许有人用不同的方法获取饼干?
您尝试过HttpContext.Current吗?如果您正在使用ASP.NET,这应该是从代码中的任何位置访问当前上下文的一种方式。如果你不是,解决方案就更棘手了。
即
HttpCookie cookie = HttpContext.Current.Request.Cookies.Get("MyCookieGitApplication");