加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-core – 使用ASP.NET Core创建cookie

发布时间:2020-12-16 06:34:18 所属栏目:asp.Net 来源:网络整理
导读:在ASP.NET MVC 5中,我有以下扩展名: public static ActionResult Alert(this ActionResult result,String text) { HttpCookie cookie = new HttpCookie("alert") { Path = "/",Value = text }; HttpContext.Current.Response.Cookies.Add(cookie); return r
在ASP.NET MVC 5中,我有以下扩展名:

public static ActionResult Alert(this ActionResult result,String text) {
  HttpCookie cookie = new HttpCookie("alert") { Path = "/",Value = text };
  HttpContext.Current.Response.Cookies.Add(cookie);
  return result;
}

基本上我正在添加一个带文本的cookie.

在ASP.NET Core中,我无法找到创建HttpCookie的方法.

这不可能吗?

解决方法

你尝试过类似的东西:

public static ActionResult Alert(this ActionResult result,Microsoft.AspNetCore.Http.HttpResponse response,string text)
    {
        response.Cookies.Append(
            "alert",text,new Microsoft.AspNetCore.Http.CookieOptions()
            {
                Path = "/"
            }
        );

        return result;
    }

您可能还必须在调用中将响应传递给控制器??(或从中调用它的任何位置).例如:

return Ok().Alert(Response,"Hi");

StackOverflow Reference

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读