SoFunction
Updated on 2025-03-08

Analysis of the difference between and in C#

.NET provides multiple ways to read and write cookies, which are cookies transmitted by the client from the client to the server through the form of a cookie header; they are created on the server and transmitted to the client in the form of a Set-Cookie header. In other words, one is sent by the client to the server, and the other is sent by the server to the client.

When creating cookies for the first time, the following two reading methods read the same content:

C# code

Copy the codeThe code is as follows:

HttpCookie hc = new HttpCookie("User2");
hc["UserName"] = "mengxianhui";
(hc);
  
["User1"]["UserId"] = "net_lover";
  
(["User1"].Values["UserId"].ToString());
(["User2"].Values["UserName"].ToString());
("<hr>");
(["User1"].Values["UserId"].ToString());
(["User2"].Values["UserName"].ToString());

However, once the cookie exists, use the above method to read it, the result is different. The new cookie can be read immediately, and the reading is still set last time, that is, it must be sent to the server through the client to read it. Why is there such a difference? It should be a problem with .NET implementation. There is a paragraph in it:
C# code

Copy the codeThe code is as follows:

if (includeResponse && ( != null))
{
    HttpCookieCollection cookies = ;
    if ( > 0)
    {
        HttpCookie[] dest = new HttpCookie[];
        (dest, 0);
        for (int i = 0; i < ; i++)
        {
            (dest[i], true);
        }
    }
}

When a cookie does not exist, it should read the cookie in it, so it should be the same for the first time, but read differently in the future.

In addition, it must be read after setting, and cannot be read at any time like that. If you read cookies on other pages, the following method is wrong

C# code

Copy the codeThe code is as follows:

protected void Page_Load(object sender, EventArgs e)
{
  (["User1"].Values["UserId"].ToString());
  (["User2"].Values["UserName"].ToString());
}

Summarize:

: It is mainly used to obtain all cookie values, including the cookie values ​​created by JS, Response??.Cookies; Speaking of this, you can see that you can obtain both cookies and cookies. So what is the use of cookies created with Response and JS? Through experiments, it was found that the cookies created can only be obtained?, while the other two methods cannot be obtained. That is to say, the cookies created can only be used in the .Net background and cannot be used in the foreground of HTML.

: It is mainly used for the creation, assignment and deletion of cookies. After the operation of cookies, all methods obtain updated values, that is, modify the values ​​of cookies in all containers; in addition to being the ultimate modifyer of cookies, when we write code, we may find that some cookies can also be obtained through the value. Through experiments, we found that the cookie value obtained here is only cookies created in this session, and is basically useless.