本文介绍了使用 ASP.NET Core 创建 cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 ASP.NET MVC 5 中,我有以下扩展:
In ASP.NET MVC 5 I had the following extension:
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.
Basically I am adding a cookie with a text.
在 ASP.NET Core 中,我找不到创建 HttpCookie 的方法.这不再可能了吗?
In ASP.NET Core I can't find a way to create the HttpCookie. Is this no longer possible?
推荐答案您是否尝试过类似的方法:
Have you tried something like:
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 传递给控制器(或您调用它的任何地方)的扩展方法.例如:
You may also have to pass the Response in your call to the extension method from the controller (or wherever you call it from). For example:
return Ok().Alert(Response, "Hi");StackOverflow 参考
更多推荐
使用 ASP.NET Core 创建 cookie
发布评论