Cookie only displayed on refresh?

前端 未结 2 1890
深忆病人
深忆病人 2021-01-26 12:45

I have some trouble understanding this one so here it is.

I\'m trying to set a cookie and display the value on the page using ASP.NET + C#.

he

2条回答
  •  青春惊慌失措
    2021-01-26 13:31

    The first time, the request has no cookies (yet); it will only have them the second time around, after the response has set them. So your code has to deal with the possibility that Request.Cookies just may not have a "fontSize" entry, and provide the proper default when that is the case. For example:

    HttpCookie cookie = Request.Cookies.Get("fontSize");
    // Check if cookie exists in the current request.
    if (cookie == null)
    {
       Response.Write( "Defaulting to 'small'.");
    }
    else
    {
       Response.Write( Request.Cookies["fontSize"].Value);
    )
    

提交回复
热议问题