Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我能';不使用HttpContext或HttpCookie?(Asp.Net Core 1.0)_C#_Asp.net_Asp.net Mvc_Cookies_Asp.net Mvc 5 - Fatal编程技术网

C# 为什么我能';不使用HttpContext或HttpCookie?(Asp.Net Core 1.0)

C# 为什么我能';不使用HttpContext或HttpCookie?(Asp.Net Core 1.0),c#,asp.net,asp.net-mvc,cookies,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Cookies,Asp.net Mvc 5,为什么我不能使用HttpContext或HttpCookie? 有特殊用途吗 我的实际使用: using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; 我的命名空间: namespace eCoffee.Main 我的类+方法: public class AppRunCookie { public static string CookieName {

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

我的实际使用:

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 "";
}
}

我做错了什么?

HttpCookie
的命名空间是
System.Web
,它是
System.Web.dll
库的一部分

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

那么你应该可以通过

using System.Web;
大家好

我找到了一个解决办法,尝试使用精彩的博客条目

注意: 确保通过nuget安装程序安装nuget软件包,并确保选中复选框“包括预发布”


希望我的帖子有帮助

你说的“不能使用”到底是什么意思?你的错误信息是什么?如果您使用的是Visual Studio,则可以通过选择带有错误的行,按ctrl+键,让VS创建缺少的using语句。并选择适当的选项。@TimPohlmann我找到了一些关于stackoverflow的教程。在本教程中,他们使用了HttpCookie语句。我试图在我自己的项目中实现这一点。VB2015告诉我:“找不到类型或命名空间HttpCookie…”你是对的,在正常情况下VB给出一些“to do”,但在这种情况下VB给出no“to do”,HttpContext方法我已经可以使用了,因为我遵循VB的“to do”。这是因为我使用了最新的.NET内核?VB吗?我想你指的是VS(又名VisualStudio)。奇怪的是,它没有提出使用方法。@TimPohlmann很抱歉我的错误:D我是说VS for Visual Studio:)如果您使用的是ASP.Net MVC,那么请注意,由于使用该框架更改了页面生命周期,因此HttpContext位于控制器部分下。可能有助于了解更多细节。根据@JB King对OP的评论,
HttpContext
将在控制器的范围内。因此,尽管
使用System.Web
将允许您访问
HttpContext
对象,但它不会与应用程序的其余部分在同一范围内,因为
HttpContext
在Controllers@GeoffJames看我的comment@Yannikhere“对不起,我不知道为什么你找不到这个集会。”蒂姆波曼;(我创建了一个普通的ASP.NET核心Web应用程序lol。请打开我的项目试一试,如果它对你有用,好吗[github]()如果你完成了,请给我一个反馈…我将提交最新版本等等…@yannik我猜,这与项目是一个ASP.NET核心Web应用程序有关。我不熟悉这些。在一个常规的C#项目中,我的解决方案工作得很好。那篇文章是用于实现会话的。但是如果你想实现cookie hand我建议使用Microsoft.AspNetCore.Http命名空间和以下类Microsoft.AspNetCore.Http.HttpResponse、Microsoft.AspNetCore.Http.HttpRequest和Microsoft.AspNetCore.Http.CookieOptions