簡體   English   中英

我如何在asp.net mvc方法中讀取cookie?

[英]How can i read cookies in asp.net mvc method?

我是asp.net mvc新手,想要讀取cookie,為此目的用以下代碼寫入cookie:

Cookies.SaveCookies(validate);


不要在這種方法中讀取cookie:

public static int CookiesReturn()
        {
            if (Request.Cookies["userName"] != null)
                return  Server.HtmlEncode(Request.Cookies["userName"].Value);

        }


但在Request.Cookies的get錯誤中:

無法解析請求

並且在這行Server.HtmlEncode...得到此錯誤:

無法解析請求

問題是Controller類有一個名為HttpContext的公共屬性(請參閱此內容 )。嘗試使用System.Web前綴。 示例 HttpContext.Current.Response.Cookies[cookieName]

所以你的代碼就像

 public static int CookiesReturn()
 {
   HttpCookie cookie =HttpContext.Current.Response.Cookies["userName"];
   if (cookie  != null)
   string username = Server.HtmlEncode(cookie.Value);
  }

您可以在鏈接中獲取有關cookie的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM