嗨我需要传递我的Request.Form作为参数,但首先我必须添加一些键/值对.我得到的例外是Collection只读.
我试过了:
System.Collections.Specialized.NameValueCollection myform = Request.Form;
Run Code Online (Sandbox Code Playgroud)
我得到同样的错误.
我试过了:
foreach(KeyValuePair<string, string> pair in Request.Form)
{
Response.Write(Convert.ToString(pair.Key) + " - " + Convert.ToString(pair.Value) + "<br />");
}
Run Code Online (Sandbox Code Playgroud)
测试我是否可以将它一个接一个地传递给另一个字典,但我得到:
System.InvalidCastException:指定的强制转换无效.
有人帮忙吗?感谢名单
Mat*_*ott 19
你不需要投射string到string.NameValueCollection围绕字符串键和字符串值构建.快速扩展方法怎么样:
public static IDictionary<string, string> ToDictionary(this NameValueCollection col)
{
var dict = new Dictionary<string, string>();
foreach (var key in col.Keys)
{
dict.Add(key, col[key]);
}
return dict;
}
Run Code Online (Sandbox Code Playgroud)
这样你就可以轻松地去:
var dict = Request.Form.ToDictionary();
dict.Add("key", "value");
Run Code Online (Sandbox Code Playgroud)
Dal*_*oft 12
如果您已经在使用MVC,那么您可以使用0行代码完成.
using System.Web.Mvc;
var dictionary = new Dictionary<string, object>();
Request.Form.CopyTo(dictionary);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13246 次 |
| 最近记录: |