Create cookie with ASP.NET Core

后端 未结 1 1787
野性不改
野性不改 2020-12-06 11:47

In ASP.NET MVC 5 I had the following extension:

public static ActionResult Alert(this ActionResult result, String text)         


        
相关标签:
1条回答
  • 2020-12-06 12:15

    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;
        }
    

    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 Reference

    0 讨论(0)
提交回复
热议问题