public class ContactsController : ApiController
{
private static readonly List<Contact> _contacts = new List<Contact>();
public Contact PutContacts(int id, Contact contact)
{
if (_contacts.Any(c => c.Id == contact.Id) == false)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
contact.LastModified = DateTime.Now;
return contact;
}
}
http put header:
PUT /api/contacts/3 HTTP/1.1
User-Agent: Fiddler
Content-Type: application/json
Host: localhost:8080
body:
{"Id":3,"Name":"mmm","Phone":"000 000 0000","Email":"[email protected]","LastModified":"2012-03-08T23:42:13.8681395+08:00"}
response:
HTTP/1.1 400 Bad Request
"The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'SelfHost.Contact PutContacts(Int32, SelfHost.Contact)' in 'SelfHost.ContactsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter."
Why? thanks.
PS:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);