微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

在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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐