Post JSON HttpContent to ASP.NET Web API

耗尽温柔 提交于 2019-12-03 05:55:13

The reason you're receiving a 404 is because the framework didn't find a method to execute given your request. By default, Web API uses the following rules to bind parameters in methods:

  • If the parameter is a “simple” type, Web API tries to get the value from the URI. Simple types include the .NET primitive types (int, bool, double, and so forth), plus TimeSpan, DateTime, Guid, decimal, and string, plus any type with a type converter that can convert from a string. (More about type converters later.)
  • For complex types, Web API tries to read the value from the message body, using a media-type formatter.

Given those rules, if you want to bind the parameter from the POST body simply add a [FromBody] attribute in front of the type:

[HttpPost]
public List<ItemInLocationModel> GetInventoryByLocationIDAndCode([FromBody] int id, string code) {
...
}

For more information please see the documentation.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!