This will give you your result:
[Route("api/createcustomer")] [HttpPost] //[ResponseType(typeof(Customer))] public IHttpActionResult CreateCustomer() { ... string location = Request.RequestUri + "/" + NewCustomer.ID.ToString(); return Created(location, new { CustomerId = NewCustomer.ID }); }
Now ResponseType does not match. If you need this attribute, you will need to create a new return type instead of using an anonymous type.
public class CreatedCustomerResponse { public int CustomerId { get; set; } } [Route("api/createcustomer")] [HttpPost] [ResponseType(typeof(CreatedCustomerResponse))] public IHttpActionResult CreateCustomer() { ... string location = Request.RequestUri + "/" + NewCustomer.ID.ToString(); return Created(location, new CreatedCustomerResponse { CustomerId = NewCustomer.ID }); }
Another way to do this is to use the DataContractAttribute in your Customer class to control serialization.
[DataContract(Name="Customer")] public class Customer { [DataMember(Name="CustomerId")] public int ID { get; set; }
Then just return the created model
return Created(location, NewCustomer); // or return CreatedAtRoute<Customer>("DefaultApi", new controller="customercontroller", CustomerID = NewCustomer.ID }, NewCustomer);
source share