How to use the cookie in the project asp.net mvc?
Loading
How to use the cookie in the project asp.net mvc?
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Jignesh KumarRank #91 · 22.9K reputationPosted Aug 9, 2024, 12:08 AM EDT
Hello Samia,
Working with ookies in MVC application,
How to set cookie :
public ActionResult SetCookie() { HttpCookie cookie = new HttpCookie("MyCookieName"); cookie.Value = "This is the value of the cookie"; cookie.Expires = DateTime.Now.AddHours(1); // Expires in 1 hour Response.Cookies.Add(cookie); return View(); }How To Read Cookie :
public ActionResult GetCookie() { // Retrieve the cookie by its name HttpCookie cookie = Request.Cookies["MyCookieName"]; if (cookie != null) { // Get the value of the cookie string cookieValue = cookie.Value; ViewBag.CookieValue = cookieValue; } else { ViewBag.CookieValue = "Cookie not found"; } return View(); }How to delete cookie :
public ActionResult DeleteCookie() { HttpCookie cookie = new HttpCookie("MyCookieName"); // Set the expiration date to a past date means it will delete cookie.Expires = DateTime.Now.AddDays(-1); // Add the cookie to the response Response.Cookies.Add(cookie); return View(); }Tahir AnsariRank #366 · 6.8K reputationPosted Aug 8, 2024, 6:51 AM EDT
Refer: https://www.aspsnippets.com/Articles/3373/ASPNet-MVC-Cookies-Read-Write-Save-and-Remove-Delete-Cookies-in-ASPNet-MVC/
https://stackoverflow.com/questions/39390240/create-cookie-asp-net-mvc
Tahir AnsariRank #366 · 6.8K reputationPosted Aug 8, 2024, 6:59 AM EDT
To create and set a cookie, you'll typically use the
HttpResponseobject. You can do this in your controller action methodpublic ActionResult SetCookie() { // Create a new cookie HttpCookie cookie = new HttpCookie("MyCookie"); cookie.Value = "This is the value of the cookie"; cookie.Expires = DateTime.Now.AddDays(7); // Set expiry date (optional) // Add cookie to response Response.Cookies.Add(cookie); return View(); }To retrieve a cookie, use the
HttpRequestobjectpublic ActionResult GetCookie() { HttpCookie cookie = Request.Cookies["MyCookie"]; if (cookie != null) { string cookieValue = cookie.Value; ViewBag.CookieValue = cookieValue; } else { ViewBag.CookieValue = "Cookie not found"; } return View(); }Mark TaborRank #914 · 2.3K reputationPosted Aug 10, 2024, 1:41 AM EDT
hi you can set the cookie like this
public ActionResult SetCookie()
{
// Create a cookie with a name and value
HttpCookie myCookie = new HttpCookie("MyCookieName");
myCookie.Value = "MyCookieValue";
// Set the expiration date
myCookie.Expires = DateTime.Now.AddDays(1); // Cookie will expire in 1 day
// Add the cookie to the response
Response.Cookies.Add(myCookie);
return RedirectToAction("Index");
}
Read the cookie like this :
public ActionResult ReadCookie()
{
HttpCookie myCookie = Request.Cookies["MyCookieName"];
if (myCookie != null)
{
string cookieValue = myCookie.Value;
ViewBag.CookieValue = cookieValue;
}
else
{
ViewBag.CookieValue = "Cookie not found";
}
return View();
}
Delete the cookie like this :
public ActionResult DeleteCookie()
{
if (Request.Cookies["MyCookieName"] != null)
{
HttpCookie myCookie = new HttpCookie("MyCookieName");
myCookie.Expires = DateTime.Now.AddDays(-1); // Set the expiration date to the past
Response.Cookies.Add(myCookie);
}
return RedirectToAction("Index");
}