[英]How to read a cookie in C#?
I'm not sure how to read a web cookie in C#.我不确定如何在 C# 中读取 web cookie。 I tried this code, but I get multiple errors.
我尝试了此代码,但出现多个错误。
private string GetCookieValue(string cookieName, string itemName)
{
var CookieName = "MY_COOKIE";
var CookieValue = string.empty;
// ^ error
HttpCookie myCookie = Request.Cookies[CookieName];
// ^ error // ^ error
if (myCookie == null)
{
return "No Cookie Found.";
}
CookieValue = myCookie.Value.ToString();
return CookieValue;
}
Any help would be appreciated, thanks.任何帮助将不胜感激,谢谢。
(sorry about that) Errors: (对此感到抱歉)错误:
line 4, 'string' does not contain a definition for 'empty' line 5, the type or namespace name 'HttpCookie' could not be found.第 4 行,“字符串”不包含“空”第 5 行的定义,找不到类型或命名空间名称“HttpCookie”。 line 5, The name Request does not exist in the current context.
第 5 行,名称 Request 在当前上下文中不存在。
You need to use string.Empty and not empty.您需要使用 string.Empty 而不是空的。 For reading of cookie, see below example:
对于 cookie 的读取,请参见以下示例:
public static string GetFromCookie(string cookieName, string keyName)
{
HttpCookie cookie = HttpContext.Current.Request.Cookies[cookieName];
if (cookie != null)
{
//Logic placeholder
}
return null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.