Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

P.S.: I am going to accept @Stripling's answer as his provided me some direction.

like image 432
Batuta Avatar asked Feb 11 '26 07:02

Batuta


1 Answers

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; }
}
like image 99
StriplingWarrior Avatar answered Feb 13 '26 01:02

StriplingWarrior