簡體   English   中英

為什么我不能使用HttpContext或HttpCookie? (Asp.Net Core 1.0)

[英]Why I can't use HttpContext or HttpCookie? (Asp.Net Core 1.0)

為什么不能使用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;

所以大家好

我找到了解決方案,請嘗試使用精彩的博客文章。

注意:請確保您通過nuget安裝程序安裝了nuget-packages,並且還確保選中了“ include prerelease”復選框。

希望我的帖子對您​​有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM