How to read MVC3 cookie?

I have a list of comment objects in the view. Each comment has one cookie that indicates the status of the user. Example: like, spam, ...

In the view, I want to read the corresponding cookie of each comment to display the correct presentation to the user. Example: user A, who liked comment B, then the view will display an unlike button

I do not want to read cookies in the controller because the returned data is a list of comment objects.

My question is, how to read cookie directly from the point of view of MVC3?

+4
source share
3 answers

In razor mode in the @ {} block, use the code below.

string val = ""; if (Request.Cookies["CookieName"] != null) { val = Request.Cookies["CookieName"].Value; } 
+11
source

to read cookies:

  var cookie = Request.Cookies["Key"]; ViewBag.MyCookie= int.Parse(cookie); 

and show it as As:

  @ViewBag.MyCookie; 
+1
source

use Request.Cookies

string val = Request.Cookies ["CookieName"] ?. Value;

0
source

Source: https://habr.com/ru/post/1500863/


All Articles