简体   繁体   English

ASP.Net Web API返回数组值

[英]ASP.Net Web API Returning Array Values

Pretty new with ASP.Net WEB API. ASP.Net WEB API相当新颖。 Having some issues with the proper API configuration (and return type) for my API call which calls another ASHX service. 我的API调用的正确API配置(和返回类型)存在一些问题,这会调用另一个ASHX服务。

I have the following codes (tested in HomeController just to verify that the service call would work): 我有以下代码(在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; }
}

Now, trying to port it over to an ASP.Net WEB API call: 现在,尝试将其移植到ASP.Net WEB API调用:

ClientAddressController.cs 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; }
}

I need some help to properly define the correct parameters for the WEB Api call so that I could return the result object. 我需要一些帮助来正确定义WEB Api调用的正确参数,以便我可以返回结果对象。

The result object would just be an array of strings: 结果对象只是一个字符串数组:

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

Hoping to get some insights on resolving this. 希望得到一些解决这个问题的见解。 I have some idea with regards to returning database queries in Web API, but the service calls (and the async method) kind of threw me off the groove. 关于在Web API中返回数据库查询,我有一些想法,但服务调用(和异步方法)让我摆脱了困境。

Thanks. 谢谢。

**UPDATE***** **更新*****

Was able to find some resolution on this and I am posting the solution I have. 能够找到一些解决方案,我发布了我的解决方案。

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; }
}

PS: I am going to accept @Stripling's answer as his provided me some direction. PS:我将接受@ Stripling的回答,因为他为我提供了一些方向。

You'll need to create a class with an Address property, and map the results to objects of that class: 您需要创建一个具有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 Classes: DTO课程:

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

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

You can return array values as dynamic List to be able to do that set method return with dynamic List. 您可以将数组值作为动态List返回,以便能够使用动态List执行set方法返回。

var resultList = new List<dynamic>();

resultList.Add(new {Address="Address 100"});
resultList.Add(new {Address="Address 200"});
resultList.Add(new {Address="Address 300"}); 

return resultList;

Hope this is what you are loooking for. 希望这是你所喜欢的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM