本文介绍了为什么我不能使用 HttpContext 或 HttpCookie?(Asp.Net 核心 1.0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么我不能使用 HttpContext
或 HttpCookie
?有什么特殊用途吗?
Why can't I use HttpContext
or HttpCookie
?Is there a special using?
我的实际用途:
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 "";
}
}
我做错了什么?
推荐答案
大家好
我找到了解决方案,尝试使用 这是精彩的博客条目.
I found a solution, try to use this wonderful blog entry.
注意:确保通过 nuget 安装程序安装 nuget 包,并确保选中包含预发布"复选框.
Note:Make sure you install the nuget-packages via the nuget installer and make also sure you checked the checkbox "include prerelease".
希望我的帖子对您有帮助
Hope my poste was helpful
这篇关于为什么我不能使用 HttpContext 或 HttpCookie?(Asp.Net 核心 1.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!