gpt4 book ai didi

c# - ASP.Net Web API 返回数组值

转载 作者:行者123 更新时间:2023-11-30 16:57:25 24 4
gpt4 key购买 nike

相当新的 ASP.Net WEB API。对于调用另一个 ASHX 服务的 API 调用,在正确的 API 配置(和返回类型)方面存在一些问题。

我有以下代码(在 HomeController 中测试只是为了验证服务调用是否有效):

public async Task<ActionResult> Index()
{
WebRequest request = WebRequest.CreateHttp("http://callme/address.ashx");

var response = await request.GetResponseAsync();
string content;

using (var stream = response.GetResponseStream())
using(var reader = new StreamReader(stream))
{
content = await reader.ReadToEndAsync();
}

var result = JsonConvert.DeserializeObject<MyResult[]>(content);

return this.View();
}

public class MyResult
{
public string ClientAddress { get; set; }
}

现在,尝试将其移植到 ASP.Net WEB API 调用:

ClientAddressController.cs

public class ClientAddressController: ApiController
{
public async IQueryable<MyResult> GetClientAddress()
{
WebRequest request = WebRequest.CreateHttp("http://callme/address.ashx");

var response = await request.GetResponseAsync();
string content;

using (var stream = response.GetResponseStream())
using(var reader = new StreamReader(stream))
{
content = await reader.ReadToEndAsync();
}

var result = JsonConvert.DeserializeObject<MyResult[]>(content);

// How to return the result object??
}
}

public class MyResult
{
public string ClientAddress { get; set; }
}

我需要一些帮助来正确定义 WEB Api 调用的正确参数,以便我可以返回结果对象。

结果对象只是一个字符串数组:

[{"Address": "Address 100"}, {"Address": "Address 200"}, {"Address": "300"}]

希望获得一些解决此问题的见解。我对在 Web API 中返回数据库查询有一些想法,但服务调用(和异步方法)有点让我偏离了轨道。

谢谢。

**更新*****

能够找到一些解决方案,我正在发布我的解决方案。

public class ClientAddressController: ApiController
{
public async Task<IHttpActionResult> GetClientAddress()
{
WebRequest request = WebRequest.CreateHttp("http://callme/address.ashx");

var response = await request.GetResponseAsync();
string content;

using (var stream = response.GetResponseStream())
using(var reader = new StreamReader(stream))
{
content = await reader.ReadToEndAsync();
}

var result = JsonConvert.DeserializeObject<MyResult[]>(content);
return Ok(result);

// How to return the result object??
}
}

public class MyResult
{
public string ClientAddress { get; set; }
}

P.S.:我将接受@Stripling 的回答,因为他为我提供了一些指导。

最佳答案

您需要创建一个具有 Address 属性的类,并将结果映射到该类的对象:

public async IQueryable<ClientAddressResult> GetClientAddress()
{
WebRequest request = WebRequest.CreateHttp("http://callme/address.ashx");

var response = await request.GetResponseAsync();
string content;

using (var stream = response.GetResponseStream())
using(var reader = new StreamReader(stream))
{
content = await reader.ReadToEndAsync();
}

IEnumerable<MyResult> result = JsonConvert.DeserializeObject<MyResult[]>(content);

return result.Select(r => new ClientAddressResult{Address = r.ClientAddress})
.AsQueryable();
}

DTO 类:

public class MyResult
{
public string ClientAddress { get; set; }
}

public class ClientAddressResult
{
public string Address { get; set; }
}

关于c# - ASP.Net Web API 返回数组值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26532524/

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