Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is ASP.Net Web API a good choice for API consumed by .Net and PHP?

In the old days, I used ASMX which provides strongly typed classes via SOAP WSDL.

I have to implement API which will be consumed by both .Net and PHP. So I already decided to go with REST (I'm really impressed by the Azure's Rest APIs such as Blob storage).

I'm pretty new to Web API although I read few books - Pro ASP.NET MVC 4, APIs, Pro ASP.NET Web API Security. However, I still can't figure out the followings -

Questions

  1. Can a client get strongly typed classes from Web API like SOAP WSDL? (If not, what do I have to do in order to get a strong typed classes like Azure Blob Storage)

  2. Can PHP client consume Web API easily? (I know nothing about PHP)

Thank you for shedding a light!

Please note that this is not an argument about SOAP vs Rest, Web API vs WCF.

like image 271
Win Avatar asked Oct 20 '22 20:10

Win


1 Answers

1) On the client side you can parse the response (json/xml) as your strongly-typed class. Code snippet from here:

You have already defined Product class in your client:

class Product
{
    public string Name { get; set; }
    public double Price { get; set; }
    public string Category { get; set; }
}

Then you call the API to get products and get the response parsed into enumerable of Products:

HttpResponseMessage response = client.GetAsync("api/products").Result;  // Blocking call!
if (response.IsSuccessStatusCode)
{
    // Parse the response body. Blocking!
    var products = response.Content.ReadAsAsync<IEnumerable<Product>>().Result;
    foreach (var p in products)
    {
        Console.WriteLine("{0}\t{1};\t{2}", p.Name, p.Price, p.Category);
    }
}

2) ASP .NET Web API is based on HTTP. So as long as your client talks HTTP you can consume the web services.

like image 151
kaptan Avatar answered Oct 27 '22 11:10

kaptan