[英]using c# webrequest to interact with an asp.net mvc 3 website
我創建了一個帶有這些操作的家庭控制器的簡單mvc3站點。
public JsonResult Param(string id)
{
string upper = String.Concat(id, "ff");
return Json(upper);
}
public ContentResult Param2(string id)
{
string upper = String.Concat(id, "ff");
return Content( upper);
}
public JsonResult Param3(string id)
{
string upper = String.Concat(id, "ff");
io gg = new io();
gg.IOName = upper;
return Json(gg);
}
}
public class io
{
public string IOName {get;set;}
}
如何使用c#webrquest獲取json並發布到這些操作URL?
WebClient比WebRequest容易得多:
using (var client = new WebClient())
{
var data = new NameValueCollection
{
{ "id", "123" }
};
byte[] result = client.UploadValues("http://foo.com/home/param", data);
}
Darin的答案很好......但是如果你想從你的方法中獲得返回JSON有效負載的結果,並且你在C#中喜歡它...我會這樣做:
var client = new WebClient();
var result = client.DownloadString("http://foo.com/home/param3/SomeID");
var serialzier = new JavaScriptSerializer();
io MyIOThing = serialzier.Deserialize<io>(result);
從那里,您可以訪問MyIOThing.IOName
。
JavaScriptSerializer
位於System.Web.Script.Serialization
命名空間中的System.Web.Extensions
程序集中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.