namevaluecollection

how to convert NameValueCollection to JSON string?

随声附和 提交于 2019-11-27 04:11:13
I tried: NameValueCollection Data = new NameValueCollection(); Data.Add("foo","baa"); string json = new JavaScriptSerializer().Serialize(Data); it returns: ["foo"] I expected {"foo" : "baa"} How do I to do this? NameValueCollection isn't an IDictionary, so the JavaScriptSerializer cannot serialize it as you expect directly. You'll need to first convert it into a dictionary, then serialize it. Update : following questions regarding multiple values per key, the call to nvc[key] will simply return them separated by a comma, which may be ok. If not, one can always call GetValues and decide what to

Getting a Request.Headers value

让人想犯罪 __ 提交于 2019-11-26 19:57:56
问题 Very simple I'm sure, but driving me up the wall! There is a component that I use in my web application that identifies itself during a web request by adding the header "XYZComponent=true" - the problem I'm having is, how do you check for this in your view? The following wont work: if (Request.Headers["XYZComponent"].Count() > 0) Nor this: if (Request.Headers.AllKeys.Where(k => k == "XYZComponent").Count() > 0) Both throw exceptions if the header variable has not been set. Any help would be

how to convert NameValueCollection to JSON string?

浪子不回头ぞ 提交于 2019-11-26 09:38:51
问题 I tried: NameValueCollection Data = new NameValueCollection(); Data.Add(\"foo\",\"baa\"); string json = new JavaScriptSerializer().Serialize(Data); it returns: [\"foo\"] I expected {\"foo\" : \"baa\"} How do I to do this? 回答1: NameValueCollection isn't an IDictionary, so the JavaScriptSerializer cannot serialize it as you expect directly. You'll need to first convert it into a dictionary, then serialize it. Update : following questions regarding multiple values per key, the call to nvc[key]