WebAPI 邮递员未调用 Post
WebAPI postman not calling Post
我在控制器上有一个 post,我不能用 post 调用它,我看不出我做错了什么。
[RoutePrefix("api/camps")]
public class CampsController : ApiController
[Route()]
public async Task<IHttpActionResult> Get()
{
//Return list
}
[Route()]
public IHttpActionResult Post(object model)
{
throw new NotImplementedException();
}
当我 运行 visual studio 中的应用程序并在 Postman 中调用 post 时
http://localhost:6600/api/camps
我得到了返回的预期列表,当设置断点时,一切都会被击中,这是我所期望的。
当我在 postman 中用 http://localhost:6600/api/camps 调用 Post 时,我输入了 Body 和 Raw。
{
"Name": "Mr A",
}
我命中了 post 方法,但类型对象的模型是空白的。
如果我将模型更改为 camp 类型,则模型的每个 属性 都为空。
我已经更改了所有大小写,并添加和删除了双引号和单引号。
我的 WebApiConfig
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
//Changes names on result to be camel case not what they are in the model.
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
//For binding to models when passing in data
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
// Web API routes
config.MapHttpAttributeRoutes();
}
使用您想要的属性创建一个 class,如下所示:
public class Your_Class
{
public string Name { get; set; }
//...Your other properties if any
}
现在在您的 API 中使用此模型 class 作为参数。
[Route()]
public IHttpActionResult Post(Your_Class model)
{
throw new NotImplementedException();
}
更新
对于仍然面临此问题的其他人,请确保在 post 请求的 header
部分中将 Content-Type
设置为 application/json
post ]man 在发送请求之前。
我在控制器上有一个 post,我不能用 post 调用它,我看不出我做错了什么。
[RoutePrefix("api/camps")]
public class CampsController : ApiController
[Route()]
public async Task<IHttpActionResult> Get()
{
//Return list
}
[Route()]
public IHttpActionResult Post(object model)
{
throw new NotImplementedException();
}
当我 运行 visual studio 中的应用程序并在 Postman 中调用 post 时 http://localhost:6600/api/camps 我得到了返回的预期列表,当设置断点时,一切都会被击中,这是我所期望的。
当我在 postman 中用 http://localhost:6600/api/camps 调用 Post 时,我输入了 Body 和 Raw。
{
"Name": "Mr A",
}
我命中了 post 方法,但类型对象的模型是空白的。 如果我将模型更改为 camp 类型,则模型的每个 属性 都为空。 我已经更改了所有大小写,并添加和删除了双引号和单引号。
我的 WebApiConfig
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
//Changes names on result to be camel case not what they are in the model.
config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
//For binding to models when passing in data
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/plain"));
// Web API routes
config.MapHttpAttributeRoutes();
}
使用您想要的属性创建一个 class,如下所示:
public class Your_Class
{
public string Name { get; set; }
//...Your other properties if any
}
现在在您的 API 中使用此模型 class 作为参数。
[Route()]
public IHttpActionResult Post(Your_Class model)
{
throw new NotImplementedException();
}
更新
对于仍然面临此问题的其他人,请确保在 post 请求的 header
部分中将 Content-Type
设置为 application/json
post ]man 在发送请求之前。