Below sample C# code demonstrated on how to create and set cookie in ASP.NET MVC Action method and pass it to Razor:
public ActionResult Index()
{
HttpCookie cookie = new HttpCookie("cookie_name",
"cookie_value");
cookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(cookie);
return View();
}
In detail:
HttpCookie cookie = HttpContext.Request.Cookies.Get("cookie_name");
HttpContext.Request.Cookies["cookie_name"] != null
HttpCookie cookie = new HttpCookie("cookie_name");
HttpContext.Response.Cookies.Remove("cookie_name");
HttpContext.Response.SetCookie(cookie );
Add comment