gpt4 book ai didi

c# - 为什么我不能使用 HttpContext 或 HttpCookie? (ASP.Net 核心 1.0)

转载 作者:太空狗 更新时间:2023-10-30 01:32:05 24 4
gpt4 key购买 nike

为什么我不能使用 HttpContextHttpCookie?有什么特殊用途吗?

我的实际使用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

我的命名空间:

namespace eCoffee.Main

我的类+方法:

public class AppRunCookie
{
public static string CookieName { get; set; }
public static string Key { get; set; }
public AppRunCookie(string key)
{
CookieName = "MyCookie_" + key;
}

public static void SetCookie(string key, int cookieExpireDate = 30)
{
HttpCookie myCookie = new HttpCookie(CookieName);
myCookie["Key"] = key;
myCookie.Expires = DateTime.Now.AddDays(cookieExpireDate);
HttpContext.Current.Response.Cookies.Add(myCookie);
}

public string GetCookie()
{
HttpCookie myCookie = HttpContext.Current.Request.Cookies[CookieName];
if (myCookie != null)
{
string key = Convert.ToString(myCookie.Values["Key"]);
return key;
}
return "";
}
}

我做错了什么?

最佳答案

HttpContextHttpCookie 的命名空间是 System.Web,它是 System.Web.dll 的一部分> 图书馆。

右键单击项目 References 并选择 Add References...。在新打开的窗口中单击 Assemblies,然后搜索(通过右上角的搜索栏)System.Web

那么你应该可以通过

using System.Web;

关于c# - 为什么我不能使用 HttpContext 或 HttpCookie? (ASP.Net 核心 1.0),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38346333/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com