在过去,我使用ASMX,它通过SOAP WSDL提供强类型类。
我必须实现API,它将被.Net和PHP使用。所以我已经决定使用REST(Azure的REST API给我留下了深刻的印象,比如Blob存储)。
我对Web API还很陌生,虽然我读过几本书——ProASP.NETMVC4,API,ProASP.NETWeb API Security。然而,我仍然无法弄清楚以下内容-
问题
-
客户端是否可以从Web API(如SOAP WSDL)获得强类型类?(如果没有,我必须做什么才能获得像Azure Blob Storage这样的强类型类)
-
PHP客户端能否轻松使用Web API(我对PHP一无所知)
谢谢你给我一盏灯!
请注意,这不是关于SOAP与Rest、Web API与WCF的争论
1)在客户端,您可以将响应(json/xml)解析为强类型类。这里的代码片段:
您已经在客户端中定义了产品类别:
class Product
{
public string Name { get; set; }
public double Price { get; set; }
public string Category { get; set; }
}
然后调用API获取产品,并将响应解析为可枚举的产品:
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.NETWeb API是基于HTTP的。因此,只要您的客户端使用HTTP,您就可以使用web服务。