Object 在 Web API 中使用 POSTMAN 进行模型绑定后为空
Object is null after model binding in Web API, using POSTMAN
我正在尝试学习 Web API 并创建了我的第一个项目,如下所示。我正在使用 postman 对其进行测试。 post 方法工作正常,我收到了响应消息——但控制器收到的 post 操作的输入为空。需要做什么才能在控制器中获取 post 值?
using System.Collections.Generic;
using System.Net.Http;
using System.Web.Http;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class ValuesController : ApiController
{
List<Comment> comments;
// GET api/values
public IEnumerable<Comment> Get()
{
return comments;
}
// GET api/values/5
public Comment Get(int id)
{
Comment c = comments[id-1];
if (string.IsNullOrEmpty(c.Description))
{
throw new HttpResponseException(System.Net.HttpStatusCode.NotFound);
}
return c;
}
// POST api/values
public HttpResponseMessage Post(Comment inputComment)
{
Comment c = new Comment();
if (inputComment != null)
{
c.Description = inputComment.Description;
c.ID = inputComment.ID;
}
//var response = new HttpResponseMessage(HttpStatusCode.Created);
//return response;
var response = Request.CreateResponse<Comment>(System.Net.HttpStatusCode.Created, c);
response.Headers.Location=new System.Uri(Request.RequestUri,"/api/values/"+c.ID.ToString());
return response;
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
public ValuesController()
{
comments = new List<Comment>();
Comment comment1 = new Comment();
comment1.ID = 1;
comment1.Description = "Test1";
Comment comment2 = new Comment();
comment2.ID = 2;
comment2.Description = "";
comments.Add(comment1);
comments.Add(comment2);
}
}
}
邮递员[=38=]
POSTMAN 请求Header
更新
在请求body中使用'raw'后,效果很好。在 POSTMAN 中,当我单击 "Generate Code" 时,它现在显示正确的 headers。
使用 Raw 作为主体类型而不是 Form-data 并输入您 JSON。
我正在尝试学习 Web API 并创建了我的第一个项目,如下所示。我正在使用 postman 对其进行测试。 post 方法工作正常,我收到了响应消息——但控制器收到的 post 操作的输入为空。需要做什么才能在控制器中获取 post 值?
using System.Collections.Generic;
using System.Net.Http;
using System.Web.Http;
using WebApplication1.Models;
namespace WebApplication1.Controllers
{
public class ValuesController : ApiController
{
List<Comment> comments;
// GET api/values
public IEnumerable<Comment> Get()
{
return comments;
}
// GET api/values/5
public Comment Get(int id)
{
Comment c = comments[id-1];
if (string.IsNullOrEmpty(c.Description))
{
throw new HttpResponseException(System.Net.HttpStatusCode.NotFound);
}
return c;
}
// POST api/values
public HttpResponseMessage Post(Comment inputComment)
{
Comment c = new Comment();
if (inputComment != null)
{
c.Description = inputComment.Description;
c.ID = inputComment.ID;
}
//var response = new HttpResponseMessage(HttpStatusCode.Created);
//return response;
var response = Request.CreateResponse<Comment>(System.Net.HttpStatusCode.Created, c);
response.Headers.Location=new System.Uri(Request.RequestUri,"/api/values/"+c.ID.ToString());
return response;
}
// PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
public ValuesController()
{
comments = new List<Comment>();
Comment comment1 = new Comment();
comment1.ID = 1;
comment1.Description = "Test1";
Comment comment2 = new Comment();
comment2.ID = 2;
comment2.Description = "";
comments.Add(comment1);
comments.Add(comment2);
}
}
}
邮递员[=38=]
POSTMAN 请求Header
更新
在请求body中使用'raw'后,效果很好。在 POSTMAN 中,当我单击 "Generate Code" 时,它现在显示正确的 headers。
使用 Raw 作为主体类型而不是 Form-data 并输入您 JSON。