【问题标题】:using c# webrequest to interact with an asp.net mvc 3 website使用 c# webrequest 与 asp.net mvc 3 网站交互
【发布时间】:2011-10-15 18:34:50
【问题描述】:

我创建了一个简单的 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 ???

【问题讨论】:

    标签: c# asp.net-mvc json webrequest


    【解决方案1】:

    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.Extensions 程序集的 System.Web.Script.Serialization 命名空间中。

    【讨论】:

      【解决方案2】:

      WebClientWebRequest 容易得多:

      using (var client = new WebClient())
      {
          var data = new NameValueCollection
          {
              { "id", "123" }
          };
          byte[] result = client.UploadValues("http://foo.com/home/param", data);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-09-10
        • 1970-01-01
        • 2015-06-09
        • 2018-07-10
        • 1970-01-01
        • 2020-08-31
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多