我已经决定在我正在开发的应用程序中使用Web (作为中间层),但似乎不知道如何将它“绑定”到前端(前端是ASP.NET MVC4项目)。通常,我只需在前端右键单击Services,选择“”,并将我的服务的URL放入其中。但是使用Web,我无法做到这一点。在我的Web API中创建一个客户机代理类,在我的前端使用哪些选项,为什么不以添加WCF或ASMX的方式将一个Web API支持作为引用添加?
发布于 2013-09-11 15:11:59
你是说Rest Web服务吗?对于Rest,没有服务定义页面,比如WCF或ASMX。通常,人们希望在JSON中使用Rest。但是..。如果您只是在寻找JSON输出,并且希望您的客户能够快速地连接到您的服务,那么应该考虑OData。它非常容易创建,它使您的数据层可以被大量的客户端语言访问。他们为大量的语言移植了OData客户端库。应要求作为答复提交。:)
发布于 2013-09-11 15:06:35
为什么不以添加WCF或ASMX的方式将Web API支持添加为引用
WCF或基于ASMX的web服务是基于SOAP的,通常有一个相关的WSDL。WSDL允许围绕生成代理类和所有这些构建工具,但是ASP.NET Web的目的是构建REST (或基于HTTP )服务,并且没有任何类似的元数据,因此通过VS添加服务引用不适用于ASP.NET Web。WADL ()本应是REST的WSDL,但该规范没有实现。
发布于 2017-10-17 22:57:21
在这里可以找到一个通用的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<T> : 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<T> GetAll()
{
var response = client.GetAsync(ServiceBaseUri).Result;
if(response.IsSuccessStatusCode)
{
return response.Content.ReadAsAsync<List<T>>().Result as List<T>;
}
else if (response.StatusCode != HttpStatusCode.NotFound)
{
throw new InvalidOperationException("Get failed with " + response.StatusCode.ToString());
}
return null;
}
public T GetById<I>(I Id)
{
if (Id == null)
return default(T);
var response = client.GetAsync(ServiceBaseUri.AddSegment(Id.ToString())).Result;
if (response.IsSuccessStatusCode)
{
return response.Content.ReadAsAsync<T>().Result as T;
}
else if (response.StatusCode != HttpStatusCode.NotFound)
{
throw new InvalidOperationException("Get failed with " + response.StatusCode.ToString());
}
return null;
}
public void Edit<I>(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>(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;
}
}
}https://stackoverflow.com/questions/18744407
复制相似问题