使用ASP.NET Core创建cookie

Mig*_*ura 6 asp.net-core

在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;
}
Run Code Online (Sandbox Code Playgroud)

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

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

这不可能吗?

小智 11

你尝试过类似的东西:

    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;
    }
Run Code Online (Sandbox Code Playgroud)

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

return Ok().Alert(Response, "Hi");
Run Code Online (Sandbox Code Playgroud)

StackOverflow参考


归档时间:

查看次数:

18439 次

最近记录:

9 年,2 月 前