使用 WebClient 发布 C# 对象的集合
我正在尝试从另一个操作(单独的站点)发布到 ASP.NET MVC 操作,并且我需要发布对象集合。我无法找到如何序列化这个对象集合,因此我可以将它们作为标准 NameValueCollection 获取。示例:
var test1 = new TestObject { FirstName = "John", LastName="Smith", IDNum=12345 };
var test2 = new TestObject { FirstName = "Betty", LastName="Jones", IDNum=34567};
var test3 = new TestObject { FirstName = "Bobby", LastName="Hebert", IDNum=9876 };
List<TestObject> coll;
coll.Add(test1);
coll.Add(test2);
coll.Add(test3);
WebClient wc = new WebClient();
wc.UploadData("http://mysite.com", ??? );
// or
wc.UploadValues("http://mysite.com", ??? );
// or...
// ?????
任何帮助将不胜感激。提前致谢。
I am trying to post to an ASP.NET MVC action from another Action (separate sites) and I need to post a collection of objects. I'm unable to find how to serialize this collection of objects so I can get them as a standard NameValueCollection. Example:
var test1 = new TestObject { FirstName = "John", LastName="Smith", IDNum=12345 };
var test2 = new TestObject { FirstName = "Betty", LastName="Jones", IDNum=34567};
var test3 = new TestObject { FirstName = "Bobby", LastName="Hebert", IDNum=9876 };
List<TestObject> coll;
coll.Add(test1);
coll.Add(test2);
coll.Add(test3);
WebClient wc = new WebClient();
wc.UploadData("http://mysite.com", ??? );
// or
wc.UploadValues("http://mysite.com", ??? );
// or...
// ?????
Any help would be appreciated. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需这样做 -
wc.Headers["Content-type"] = "application/x-www-form-urlencoded";
wc.UploadString("url", "postData");
Just do this -
wc.Headers["Content-type"] = "application/x-www-form-urlencoded";
wc.UploadString("url", "postData");