gpt4 book ai didi

asp.net-mvc-4 - WebApi放置 "The parameters dictionary contains a null entry for parameter..."

转载 作者:行者123 更新时间:2023-12-02 10:47:03 24 4
gpt4 key购买 nike

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 放置 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."
为什么?谢谢。

附注:

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

最佳答案

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

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

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

我还在研究...

关于asp.net-mvc-4 - WebApi放置 "The parameters dictionary contains a null entry for parameter...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9621255/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com