简体   繁体   中英

ASP.Net Web API Returning Array Values

Pretty new with ASP.Net WEB API. Having some issues with the proper API configuration (and return type) for my API call which calls another ASHX service.

I have the following codes (tested in HomeController just to verify that the service call would work):

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:

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.

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.

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.

You'll need to create a class with an Address property, and map the results to objects of that class:

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:

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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