gpt4 book ai didi

asp.net - 如何在 asp.net mvc 3 中使用非 wcf 休息服务?

转载 作者:行者123 更新时间:2023-12-02 11:05:59 25 4
gpt4 key购买 nike

我有一个使用 Webmatrix 编写的简单 Web 服务,它以 json 形式返回数据。我喜欢使用 ASP.NET MVC 3 使用该服务。我知道如何使用 WCF 执行此操作,但我们不会在此项目中使用 WCF。是否有类似于 ASP.NET MVC 中 jquery 的 getJson() 方法的东西,我只需传入restful url,它就会返回数据,然后我在回调中处理它?感谢您的帮助:-)

这是我的服务:

网址:

 /services/GetAllItemsService

代码:

@{
var items = ItemsService.GetAllItems();

Json.Write(items, Response.Output);
}

最佳答案

您可以使用WebClient从远程资源获取 JSON 数据。例如:

using (var client = new WebClient())
{
string json = client.DownloadString("http://example.com/services/GetAllItemsService");

// TODO: do something with this JSON data, like for example deserialize into a model
var serializer = new JavaScriptSerializer();
var model = serializer.Deserialize<SomeModel>(json);
}

或者,如果您想将 JSON 直接写入输出:

using (var client = new WebClient())
{
string json = client.DownloadString("http://example.com/services/GetAllItemsService");
Response.Output.Write(json);
}

关于asp.net - 如何在 asp.net mvc 3 中使用非 wcf 休息服务?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6846029/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com