为什么我们不能像在WCF或ASMX中那样在Visual Studio中添加Web API作为“服务引用”?

我决定将Web API(作为中间层)用于我正在开发的应用程序,但似乎无法弄清楚如何将其“绑定”到前端(前端是ASP.NET MVC4项目)。 通常,我只需右键单击前端的服务,选择“添加服务引用”,然后将我的服务的URL放入。但是使用Web API,我不能这样做。 从我的前端使用的Web API创建客户端代理类有哪些选择,为什么不像添加WCF或ASMX那样将Web API支持作为引用添加?

你是说Rest Web Service吗? 使用Rest,没有服务定义页面,如WCF或ASMX。 通常人们希望使用带有JSON的Rest API ..但是..如果您只是在寻找JSON输出,并且您希望您的客户能够快速连接到您的服务,那么您应该考虑使用OData。 它非常容易创建,它使您的数据层可以访问大量客户端语言。 他们将OData客户端库移植到大量语言中。 根据要求提交答案。 🙂

为什么不像添加WCF或ASMX那样添加Web API支持作为引用

基于WCF或ASMX的Web服务是基于SOAP的,通常存在关联的WSDL。 WSDL允许构建工具以生成代理类,但ASP.NET Web API旨在构建基于REST(或基于HTTP)的服务,并且没有WSDLforms的元数据或任何类似的元数据,因此添加服务引用通过VS不适用于ASP.NET Web API。 WADL(Web应用程序描述语言)应该是REST的WSDL,但那个规范没有。

这里有一个通用的WebAPI客户端:

https://github.com/CamSoper/CamTheGeek

根据要求,它不是代理,但确实填补了空白。

这是源代码:

using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Net; using System.Net.Http; using System.Web; namespace CamTheGeek { public class GenericWebApiClient : IDisposable where T : class { HttpClient client = new HttpClient(); Uri ServiceBaseUri; public GenericWebApiClient(Uri ServiceUri) { if(ServiceUri == null) { throw new UriFormatException("A valid URI is required."); } ServiceBaseUri = ServiceUri; } public List GetAll() { var response = client.GetAsync(ServiceBaseUri).Result; if(response.IsSuccessStatusCode) { return response.Content.ReadAsAsync>().Result as List; } else if (response.StatusCode != HttpStatusCode.NotFound) { throw new InvalidOperationException("Get failed with " + response.StatusCode.ToString()); } return null; } public T GetById(I Id) { if (Id == null) return default(T); var response = client.GetAsync(ServiceBaseUri.AddSegment(Id.ToString())).Result; if (response.IsSuccessStatusCode) { return response.Content.ReadAsAsync().Result as T; } else if (response.StatusCode != HttpStatusCode.NotFound) { throw new InvalidOperationException("Get failed with " + response.StatusCode.ToString()); } return null; } public void Edit(T t, I Id) { var response = client.PutAsJsonAsync(ServiceBaseUri.AddSegment(Id.ToString()), t).Result; if (!response.IsSuccessStatusCode) throw new InvalidOperationException("Edit failed with " + response.StatusCode.ToString()); } public void Delete(I Id) { var response = client.DeleteAsync(ServiceBaseUri.AddSegment(Id.ToString())).Result; if (!response.IsSuccessStatusCode) throw new InvalidOperationException("Delete failed with " + response.StatusCode.ToString()); } public void Create(T t) { var response = client.PostAsJsonAsync(ServiceBaseUri, t).Result; if (!response.IsSuccessStatusCode) throw new InvalidOperationException("Create failed with " + response.StatusCode.ToString()); } public void Dispose(bool disposing) { if (disposing) { client = null; ServiceBaseUri = null; } } public void Dispose() { this.Dispose(false); GC.SuppressFinalize(this); } ~GenericWebApiClient() { this.Dispose(false); } } static class UriExtensions { public static Uri AddSegment(this Uri OriginalUri, string Segment) { UriBuilder ub = new UriBuilder(OriginalUri); ub.Path = ub.Path + ((ub.Path.EndsWith("/")) ? "" : "/") + Segment; return ub.Uri; } } }