Asp.Net MVC Add Cookie and Model List
In this article, we will see the use of Cookie (cookie) code that we will create with Asp.Net MVC in C # code.
Cookies , small files that are stored by browsers when a web page is visited. Keeps track of what people search on your web sites in browser history. Allows a website to keep track of site movements in browser logs.
Cookies Where are cookies used?
- In sites requiring membership;The sites that are required for membership are at a loss while providing us with ease of use.
- On e-commerce sites;Cookies are used in e-commerce sites to remember half your basket.
- In ad impressions;Cookies are also tools that allow us to show ads based on our interests. What sites are we entering during the day, what are we clicking on the sites we enter, what are we looking at from e-commerce sites are all recorded as snacks. These saved cookies allow us to show ads based on our interests.
On the program side, let’s look at the Cookies that we use to help with the operations we do with the software, and the C # code in Asp.Net MVC.
Add Cookie
- File:HomeController.cs
2 3 4 5 6 7 8 9 10 |
public void AddCookie(string cookieName, string cookieValue) { // Add Cookie //cookieName :cookie name to add //cookieValue : cookie value to add HttpCookie cookie = new HttpCookie(cookieName, cookieValue); cookie.Expires = DateTime.Now.AddYears(1); // Time(1 years) HttpContext.Response.Cookies.Add(cookie); } |
Your cookie insertion process has been successfully performed in the above function.
Delete Cookie
2 3 4 5 6 7 8 |
public void DeleteCookie(string userCookieName) { //Delete Cookie HttpCookie myCookie = new HttpCookie(userCookieName); myCookie.Expires = DateTime.Now.AddYears(-1);// we reduce the period by 1 year Response.Cookies.Add(myCookie); } |
- The cookie clearing process was performed.
Get Cookie
2 3 4 5 6 7 |
public string GetCookie(string cookieName) { // pull value by cookie name string cookie = HttpContext.Request.Cookies[cookieName].Value; return cookie; } |
Let’s look at the output by adding a cookie. If login actionu is created and login process is completed, I will invoke the method of adding cookies.
2 3 4 5 6 |
public ActionResult Login() { AddCookie("username", "Hello world"); return View(); } |
Add Model List in Cookie
2 3 4 5 6 7 8 9 10 11 12 13 14 |
public static void AddCookie(List<PRODUCTS> basketList) { HttpCookie cookie = new HttpCookie("MarketListCookie"); string objCartListString = ""; foreach (var item in basketList) { objCartListString += string.Join(",", item.FEATUREID, item.PRODUCT_NAME, item.DISCOUNT_PRICE, item.PRODUCT_PRICE, item.PRODUCT_PIECE) + "|"; } HttpContext.Current.Response.Cookies["MarketListCookie"].Value = objCartListString; cookie.Value = HttpContext.Current.Response.Cookies["MarketListCookie"].Value; cookie.Expires = DateTime.Now.AddYears(30); HttpContext.Current.Response.Cookies.Add(cookie); } |
- In the above process we send our model list.
Get Model List in Cookie
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public static List<PRODUCTS> GetCookie(string name) { HttpCookie cookie = HttpContext.Current.Request.Cookies[name]; List<PRODUCTS> basketList = new List<PRODUCTS>(); if (cookie != null) { string objCartListString = cookie.Value.ToString(); string[] objCartListStringSplit = objCartListString.Split('|'); foreach (string s in objCartListStringSplit) { string[] ss = s.Split(','); if (ss[0] != "") { PRODUCTS model = new PRODUCTS() { FEATUREID = Convert.ToInt32(ss[0]), PRODUCT_NAME = Convert.ToString(ss[1]), DISCOUNT_PRICE = Convert.ToInt32(ss[2]), PRODUCT_PRICE = Convert.ToInt32(ss[3]), PRODUCT_PIECE = Convert.ToInt32(ss[4]) }; basketList.Add(model); } } } return basketList; } |
- According to the rule we set the cookie read process above
Demo
2 3 4 5 |
public ActionResult Home() { MarketHelper.AddCookie(basketList); } |
As you can see we have successfully added our cookie list to our list.According to the situation we can read the list with the GetCookie method.
if you have question do not forget to write from the chat button next to it or from the comment
Good answers in return of this matter with firm arguments and explaining all on the topic of that.