为什么我不能使用HttpContext或HttpCookie? (Asp.Net Core 1.0) [英] Why I can't use HttpContext or HttpCookie? (Asp.Net Core 1.0)
本文介绍了为什么我不能使用HttpContext或HttpCookie? (Asp.Net Core 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-packages,并确保选中了"include prerelease"复选框.
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 Core 1.0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文