ASP.NET Web API - POST/PUT/DELETE Request from an ASP.NET MVC 4 app
First, check if WebApiConfig.cs
under App_Start
has the routes mapped like so (which is the default).
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
With this, routing happens based on the HTTP method. So, if you issue a GET to /api/CustomerDetail/123, your action method GetCustomerDetailById(int)
gets called. GET to /api/CustomerDetail will call GetAllCustomerDetails()
. You do not need to use the action method name in the URI though it is possible to do so.
For GET, this will work.
Task<String> response = httpClient.GetStringAsync
("http://localhost:<port>/api/CustomerDetail/123");
For POST, this will work.
HttpClient client = new HttpClient();
var task = client.PostAsJsonAsync<CustomerDetail>
("http://localhost:<port>/api/CustomerDetail",
new CustomerDetail() { // Set properties });
You can also use
var detail = new CustomerDetail() { // Set properties };
HttpContent content = new ObjectContent<CustomerDetail>(
detail, new JsonMediaTypeFormatter());
var task = client.PostAsync("api/CustomerDetail", content);
For more info, check this out.
BTW, the convention is to use a plural for the controller class name. So, your controller can be named as CustomerDetailsController or even better CustomersController, since details are what you deal with.
Also, POST is generally used for creating resources and PUT for updating resources.
lincx
Updated on June 04, 2022Comments
-
lincx almost 2 years
I have an ASP.NET Web API REST that handles the data requests from an ASP.NET MVC 4 application
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; } }
}
Now in the ASP.NET MVC4 app, I have this 'wrapper' class that calls the said WEB API, This handles the GET requests
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; } } }
Now, my problem is for the POST/PUT/DELETE REST requests
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}; }
I have tried googling this for hours now and would really appreciate if someone can point me in the right direction.