6
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 /api/contacts/3 HTTP/1.1
User-Agent: Fiddler
Content-Type: application/json
Host: localhost:8080

身体:

{"Id":3,"Name":"mmm","Phone":"000 000 0000","Email":"mmm@gmail.com","LastModified":"2012-03-08T23:42:13.8681395+08:00"}

回复:

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."

为什么?谢谢。

PS:

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
4

3 回答 3

2

我有同样的问题。查询参数“id”为空,填写对象“contact”。我认为这是模型绑定问题。如果你在控制器中放置一个断点,看这个表达式:

this.ControllerContext.RouteData.Values["id"]

您将看到该值存在于路线数据中;它只是没有在模型上设置。我有一个 ActionFilter,如果我也在那里放一个断点,我会看到 actionContext 的 ActionArgument 有一个键,但它是一个空值。

我还在研究...

于 2012-04-03T19:40:42.317 回答
1

我无法重现您描述的问题。

模型:

public class Contact
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Phone { get; set; }
    public string Email { get; set; }
    public DateTime LastModified { get; set; }
}

控制器:

public class ContactsController : ApiController
{
    public Contact Put(int id, Contact contact)
    {
        return contact;
    }
}

客户:

class Program
{
    static void Main()
    {
        using (var client = new WebClient())
        {
            client.Headers[HttpRequestHeader.ContentType] = "application/json";
            var data = Encoding.UTF8.GetBytes(@"{""Id"":3,""Name"":""mmm"",""Phone"":""000 000 0000"",""Email"":""mmm@gmail.com"",""LastModified"":""2012-03-08T23:42:13.8681395+08:00""}");
            var result = client.UploadData("http://localhost:1405/api/contacts/4", "PUT", data);
            Console.WriteLine(Encoding.UTF8.GetString(result));
        }
    }
}

当我运行客户端时,正在发送以下请求:

PUT /api/contacts/4 HTTP/1.1
Content-Type: application/json
Host: localhost:1405
Content-Length: 119
Expect: 100-continue
Connection: Keep-Alive

{"Id":3,"Name":"mmm","Phone":"000 000 0000","Email":"mmm@gmail.com","LastModified":"2012-03-08T23:42:13.8681395+08:00"}

我从服务器得到正确的结果。所以我猜你显示的请求不是发送到服务器的实际请求。

于 2012-03-08T16:49:29.637 回答
0

craigtadlock,感谢您的帮助并将错误发送给 Microsoft。我在 PUT 上遇到了同样的问题。他们都没有工作。作为一种解决方法,我破解了 POST 来处理 PUT。如果我发布到服务器的对象已经具有它的 ID,则 POST 代码会将其视为具有该 ID 的 PUT。伪代码是:

public HttpResponseMessage<MyClass> PostMyClass(MyClass myObject)
{
    if( myObject.ID != 0 ){
         //do PUT code
    }else{
         //do POST code
    }
}
于 2012-05-05T14:32:15.243 回答