使用我这个方式的前提是在mvc中,确认你安装了:Microsoft.AspNetCore.Mvc.
然后在继承了Controller的类型中使用我所说的方法。
直接使用即可,我是封装了方法供我自己使用,代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
public abstract class ControllerBase : Controller { /// <summary> /// 设置本地cookie /// </summary> /// <param name="key">键</param> /// <param name="value">值</param> /// <param name="minutes">过期时长,单位:分钟</param> protected void SetCookies(string key, string value, int minutes = 30) { HttpContext.Response.Cookies.Append(key, value, new CookieOptions{ Expires = DateTime.Now.AddMinutes(minutes) }); } /// <summary> /// 删除指定的cookie /// </summary> /// <param name="key">键</param> protected void DeleteCookies(string key) { HttpContext.Response.Cookies.Delete(key); } /// <summary> /// 获取cookies /// </summary> /// <param name="key">键</param> /// <returns>返回对应的值</returns> protected string GetCookies(string key) { HttpContext.Request.Cookies.TryGetValue(key, out string value); if (string.IsNullOrEmpty(value)) value = string.Empty; return value; } } |
from:http://www.cnblogs.com/dawenyang/archive/2018/06/25/9223331.html