ASP.NET MVC 4 アプリケーションからのデータ要求を処理する ASP.NET Web API REST があります。
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};
}
私はこれを何時間もグーグルで試してみましたが、誰かが私を正しい方向に向けることができれば本当に感謝しています.