今朝私は誤って次のスニペットコードを見ましたが、とてもうまく機能していたのでかなり驚いていました。なぜRequest.CookiesはforeachループでHttpCookieオブジェクトの代わりにstringを返しますか?
私は、HttpCookieCollection(この場合はRequest.Cookies)がforeachループ内のHttpCookieオブジェクトの代わりに文字列(Cookie名)を返すのはなぜか不思議です。このコレクション内で通常HttpCookieオブジェクトをindex/nameで取得するため、一貫性の問題ですか?
おかげで、
foreach (string cookieKey in System.Web.HttpContext.Current.Request.Cookies)
{
HttpCookie tmpCookie = System.Web.HttpContext.Current.Request.Cookies[cookieKey];
if (tmpCookie != null && tmpCookie["RecentlyVisited"] != null)
{
cookie.Add(tmpCookie);
}
}
は、 @クリスありがとうございます::これはトリックを行う必要があります
私は、forループでコレクションを反復処理する方法を尋ねていないが、) –
私は同じ問題を持って、私は理解していませんなぜforeach宣言でHttpCookieの文字列instedを使用しなければならないのですか?どんな手掛かり? – gonxalo
System.Net.CookieCollectionを使用している場合、そのように反復することができます。 HttpCookieCollectionでは、奇妙な振る舞いとは思えません。 パブリックスタティックHttpCookieCollection CookieCollectionToHttpCookieCollection(CookieCollection cookieCollection) { HttpCookieCollection httpCookieCollection = new HttpCookieCollection(); foreach(cookieCollectionのCookie Cookie) { httpCookieCollection.Add(CookieToHttpCookie(cookie)); } return httpCookieCollection; } – gonxalo