使用 ASP.NET Core 创建 cookie

编程入门 行业动态 更新时间:2024-10-21 14:45:27
本文介绍了使用 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

本文发布于:2023-11-05 07:58:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560301.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NET   ASP   cookie   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!