ASP.Net Web API Array Return Values

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

I have the following codes (tested in HomeController to make sure the service call will 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 pass it to the 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 help figuring out the correct parameters for the WEB Api call so that I can return the result object.

The result object will simply be an array of strings:

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

      

Hoping to get some information on this. I have an idea regarding returning database queries to the Web API, but the service call (and the async method) threw me off the groove.

Thank.

** UPDATE *****

It was possible to find some kind of 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'm going to accept @Stripling's answer as he gave me some direction.

+3


source to share


2 answers


You need to create a class with a property Address

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

      

+2


source


You can return array values ​​as a dynamic list so that you can return a set method with a 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 where you go.

0


source







All Articles