我有一个 ASP.NET Web API REST 来处理来自 ASP.NET MVC 4 应用程序的数据请求
namespace CandidateAPI.Controllers
{
public class CustomerDetailController : ApiController
{
private static readonly CustomerDetailRepository Repository = new CustomerDetailRepository();
public IEnumerable<CustomerDetail> GetAllCustomerDetails()
{
return Repository.GetAll();
}
public CustomerDetail GetCustomerDetailById(int id)
{
return Repository.Get(id);
}
public HttpResponseMessage PostCustomerDetail(CustomerDetail item)
{
item = Repository.Add(item);
var response = Request.CreateResponse<CustomerDetail>(HttpStatusCode.Created, item);
var uri = Url.Link("DefaultApi", new { id = item.ID });
if (uri != null) response.Headers.Location = new Uri(uri);
return response;
}
}
}
现在在 ASP.NET MVC4 应用程序中,我有一个调用上述 WEB API 的“包装器”类,它处理 GET 请求
public class CustomerDetailsService : BaseService, ICustomerDetailsService
{
private readonly string api = BaseUri + "/customerdetail";
public CustomerDetail GetCustomerDetails(int id) {
string uri = api + "/getcustomerdetailbyid?id=" + id;
using (var httpClient = new HttpClient())
{
Task<String> response = httpClient.GetStringAsync(uri);
return JsonConvert.DeserializeObjectAsync<CustomerDetail>(response.Result).Result;
}
}
}
现在,我的问题是 POST/PUT/DELETE REST 请求
public class CustomerDetailsService : BaseService, ICustomerDetailsService
{
private readonly string api = BaseUri + "/customerdetail";
// GET -- works perfect
public CustomerDetail GetCustomerDetails(int id) {
string uri = api + "/getcustomerdetailbyid?id=" + id;
using (var httpClient = new HttpClient())
{
Task<String> response = httpClient.GetStringAsync(uri);
return JsonConvert.DeserializeObjectAsync<CustomerDetail>(response.Result).Result;
}
}
//PUT
public void Add(CustomerDetail detail) { //need code here };
//POST
public void Save(CustomerDetail detail) { //need code here };
//DELETE
public void Delete(int id) { //need code here};
}
我已经尝试在谷歌上搜索了几个小时,如果有人能指出我正确的方向,我将不胜感激。