簡體   English   中英

使用c#webrequest與asp.net mvc 3網站進行交互

[英]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?

WebClientWebRequest容易得多:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM