WebApi Put“参数字典包含参数的空条目...”

时间:2012-03-08 16:35:06

标签: asp.net-mvc-4 self-hosting

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

体:

{"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 }
        );

3 个答案:

答案 0 :(得分:2)

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

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

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

我还在研究......

答案 1 :(得分: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"}

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

答案 2 :(得分: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
    }
}