【问题标题】:Using JSON data in ASP.NET MVC在 ASP.NET MVC 中使用 JSON 数据
【发布时间】:2012-05-23 02:50:32
【问题描述】:

我以前没有这样做过,所以需要一些线索。我有一个 ASP.NET MVC4(测试版)项目 - 移动项目 - 设置。我得到了一组 REST API 来使用。我该怎么做? API 以 JSON 格式返回数据。您有任何示例、最佳做法...?

【问题讨论】:

    标签: asp.net-mvc json jquery-mobile


    【解决方案1】:

    您可以使用$.ajax 方法向服务器上的 Web Api 控制器发送 AJAX 请求:

    $.ajax({
        url: 'api/values/123',
        type: 'GET',
        success: function(data) {
            // if the controller returned JSON data, the data argument
            // will represent a javascript object that you could directly
            // access its properties 
        }
    });
    

    【讨论】:

    • 谢谢达林。那么如何使用 ASP.NET MVC 中的数据呢?这些示例表明我不需要“ASP.NET MVC”-ish 代码来使用数据......我们只需要 jQuery。我说的对吗?
    • 是的,你是对的。如果您想从客户端使用服务,请使用 javascript。如果您想从 Web 应用程序中使用它,您可以使用 HttpClient,它是 Web API 的客户端框架:johnnycode.com/blog/2012/02/23/…
    • 谢谢达林。标记为答案。
    【解决方案2】:

    使用WebClient 类的DownloadString 方法。这将为您提供 RESTURL 返回的字符串输出。您可以将其转换为 Json 并迭代结果

    string address="http://yourrestdomain/customer/234";
    WebClient client = new WebClient ();
    string reply = client.DownloadString (address);
    

    【讨论】:

      猜你喜欢
      • 2016-11-27
      • 1970-01-01
      • 2015-09-28
      • 2020-11-29
      • 2019-12-11
      • 2018-09-02
      • 2017-07-22
      • 2020-03-18
      • 1970-01-01
      相关资源
      最近更新 更多