在 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/