2016-10-25 7 views
2

In ASP.NET MVC 5 Ich hatte die folgende Erweiterung:Cookie mit ASP.NET Core-

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

Grundsätzlich Ich füge ein Cookie mit einem Text.

In ASP.NET Core kann ich keinen Weg finden, den HttpCookie zu erstellen.

Ist das nicht möglich?

+1

Mögliche Duplikat [Plätzchen in ASP.NET Core-RC2] (http://stackoverflow.com/questions/37958343/cookies-in-asp-net-core-rc2) –

+0

Mögliches Duplikat von [Cookies und ASP.NET Core] (https://stackoverflow.com/questions/36166075/cookies-and-asp-net-core) –

Antwort

3

Haben Sie versucht, so etwas wie:

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

Sie auch die Antwort in Ihrem Aufruf der Erweiterungsmethode von der Steuerung zu übergeben kann (oder wo auch immer Sie es von nennen). Zum Beispiel:

return Ok().Alert(Response, "Hi"); 

StackOverflow Reference

+0

Antwort wird nicht innerhalb der Erweiterungen erkannt ... –

Verwandte Themen