c# - 使用 ASP.NET Core 创建 cookie

标签 c# 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;
}
基本上我正在添加一个带有文本的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;
    }

您可能还必须在您的调用中将 Response 传递给 Controller ​​(或您调用它的任何地方)的扩展方法。例如:
return Ok().Alert(Response, "Hi");

StackOverflow Reference

关于c# - 使用 ASP.NET Core 创建 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56460304/

相关文章:

asp.net - 如何在powershell中执行具有[[FromForm]]参数的API PUT方法

c# - 为特定文件禁用 Resharper 的 "Adjust namespace"

c# - 在 Windows 8 Pro 平板电脑中使用 WinAPI 抛出 AccessViolationException

asp.net-core - HttpRequestMessage.Content 在接收 Controller 操作时为空

c# - 信号R : Access Hub class from another project

c# - Microsoft ASP.NET 和 Web 工具 2015 - DNX 和 DNVM

c# - 为什么我的 RunWorkerCompleted 没有收到 RunWorkerCompletedEventArgs?

c# - 如何使用 List<Tuple<>> 作为操作方法参数?

c# - 用 2 个数字之间的所有数字填充下拉列表

c# - 如果 IHostedService.StopAsync 方法的实现不响应取消请求,会发生什么情况?