网络平台
对于我使用的每个 appSetting,我想指定一个值,如果在 appSettings 中找不到指定的键,则将返回该值。我本来打算创建一个类来管理它,但我想这个功能可能已经在 .NET Framework 中的某个地方了?
.NET 中是否有一个 NameValueCollection/Hash/etc 类型的类,可以让我指定一个键和一个后备/默认值——并返回键的值或指定的值?
如果有,我可以在调用它之前(从不同的地方)将 appSettings 放入该类型的对象中。
我正试图帮助儿子升级我去年为他建立的网站。他想实施Amazon Simple Pay。我现在很接近,但是有一个我不知道如何解决的错误。这是用C#完成的ASP.Net网站。我是未经培训(自学)的开发人员,所以请简单地说。;-)
在ASP.Net中,在表单中包含表单是不合法的,我需要执行表单POST。在线上有一个漂亮的教程,展示了如何做到这一点。网址是http://weblogs.asp.net/hajan/archive/2011/04/20/amazon-simple-pay-in-asp-net.aspx,如果你有兴趣看到它。
必须对交易进行“签名”,Amazon提供一个SignatureUtils类来完成此任务。在那堂课中,我称之为:
public static string signParameters(IDictionary<String, String> parameters, String key, String HttpMethod, String Host, String RequestURI, String algorithm)
Run Code Online (Sandbox Code Playgroud)
杀死我的是IDictionary参数。我需要传递的是我使用的ListParams NameValueCollection:
public System.Collections.Specialized.NameValueCollection ListParams = new System.Collections.Specialized.NameValueCollection();
Run Code Online (Sandbox Code Playgroud)
它给我下面的错误,因为它不能将NameValueCollection转换为IDictionary。我尝试显式转换它,但没有任何乐趣。我该如何解决?
Error: Argument 1: cannot convert from 'System.Collections.Specialized.NameValueCollection' to 'System.Collections.Generic.IDictionary<string,string>'
Run Code Online (Sandbox Code Playgroud) 我试图使用C#修改HTTP标头.我试图在Page preinit事件上操作Request.Headers.但是当我尝试向Headers设置任何内容时,我得到PlatformNotSupportedException.由于我们无法将新的NameValueCollection设置为Reqeust.Headers,因此我尝试使用以下代码设置该值:
Request.Headers.Set(HttpRequestHeader.UserAgent.ToString(), "some value");
Run Code Online (Sandbox Code Playgroud)
不知道如何实现这一目标?
有没有办法从NameValueCollection中提取与某个模式/命名约定相关的键,而不必迭代集合中的每个值?
我已经读过NameValueCollection允许添加重复键,但是当我尝试使用它时似乎不是这种情况.
我的守则
using (var wb = new WebClient())
{
var data = new NameValueCollection();
var sourceData = (List<Dictionary<string, object>>)dic["mapData"];
var countSource = sourceData.Count;
foreach (var item in (List<Dictionary<string, object>>)dic["mapData"])
{
data.Add("pp", item["Latitude"].ToString() + "," + item["Longitude"].ToString());
}
var dataCount = data.Count;
var response = wb.UploadValues(@"http://dev.virtualearth.net/REST/V1/Imagery/Map/road/?mapArea=" + swLat.ToString() + "," + swLong + "," + neLat + "," + neLong + "&mapSize=800,600&key=" + key, "POST", data);
return this.LargeJson(new { imageData = CreateBase64Image(response) });
}
Run Code Online (Sandbox Code Playgroud)
我在观察什么
我的sourceData包含36个项目

我正在迭代sourceData并将项添加到我的NameValueCollection数据并添加具有相同键"pp"的项目
我期待在我的数据NameValueCollection中有36个项目,但我只得到1并且我的所有值都被附加到同一个键.
我错过了什么?
如何转换 NameValueCollection,例如:
var mydata = new NameValueCollection();
mydata.Add("UserID", "3698521478963");
mydata.Add("Password", "23584");
mydata.Add("VerifyCode", "23654");
Run Code Online (Sandbox Code Playgroud)
像这样的 XML 字符串:
XML 数据:
我正在开发一个ASP.Net Web API项目.在我的项目中,我试图以这种格式从动作中返回JSON.
{
"Apple": null,
"Microsoft": null,
"Google": 'http://placehold.it/250x250'
}
Run Code Online (Sandbox Code Playgroud)
正如您在JSON中看到的那样,它是键值对.键和值都是动态的.所以我试图NameValueCollection从行动中返回以获得该格式.请参阅下面的代码.
public class CompaniesController : ApiController
{
// GET api/<controller>
public HttpResponseMessage Get()
{
NameValueCollection collection = new NameValueCollection();
collection.Add("Microsoft", "microsoft.com");
collection.Add("Google", "google.com");
collection.Add("Facebook", "facebook.com");
return Request.CreateResponse(HttpStatusCode.OK, collection, "application/json");
}
}
Run Code Online (Sandbox Code Playgroud)
但后来我访问了该操作,它返回JSON,如下所示.
如您所见,它只返回Keys.姓名不包括在内.那不是我想要的格式.请问如何修复我的代码以获取返回的名称值对.
在我尝试使用内部 NameValueCollection 初始化 NameValueCollection 之后,c# 编译器说它必须是 NameValueCollection(string,string)。
正如我在像这样初始化时调用 Add 函数时所想的那样。
有没有其他方法可以基于 NameValueCollection 在一行上初始化相同的方法?
原因是我必须将数据 POST 到一个 HTTPAddress 并且我使用这个函数
这是我的示例,它不起作用:
NameValueCollection data = new NameValueCollection()
{
{ "key1", "value1"},
{ "key2", new NameValueCollection()
{
{ "key2sub", "value"}
}
};
Run Code Online (Sandbox Code Playgroud) 我们想从NameValueCollection中删除几个键,但我们不确定它们是否确实存在于其中.
如果我尝试删除不在NameValueCollection中的key1,则没有异常/副作用:
nameValues.Remove("key1");
Run Code Online (Sandbox Code Playgroud)
但是什么是理想的方法,我们应该在删除之前检查密钥是否存在?
我有一行简单的代码: lead.InternalCompany = nvCollection["ic"];
我想设置lead.InternalCompany为检索到的值,但如果没有任何空白字符串""
我试过用 nvCollection["ic"].HasValue();
我知道我可以做这样简单的事情
string value = nvCollection["ic"];
if (value == null) // key doesn't exist
{
lead.InternalCompany = "";
}
Run Code Online (Sandbox Code Playgroud)
理想情况下,我希望if statement能够实现这一目标
我有一个NameValueCollection,我需要将其转换为Map,我无法解决它.我试过了:
let headerMap (m : MailMessage) = m.Headers |> Map.map (fun k v -> v.[k])
Run Code Online (Sandbox Code Playgroud)
我需要使用Seq.map吗?
基本上,我要将System.Net.MailMessage中的头文件序列化为JSON.
我正在使用Sitecore 7.1.我使用了一个字段类型 "名称查找值列表".我希望在MVC中代码后面的字段类型中存储catch名称/值.如何捕获键值项.
sitecore namevaluecollection asp.net-mvc-3 sitecore7 sitecore7.1
c# ×9
asp.net ×3
.net ×2
appsettings ×1
f# ×1
http-headers ×1
json ×1
keyvaluepair ×1
mailmessage ×1
map ×1
sitecore ×1
sitecore7 ×1
sitecore7.1 ×1
xml ×1