[英]Get body of request in ASP.NET Web Api
I faced problem like this. 我遇到了这样的问题。
I have method in controller, that receive data in body of POST request. 我在控制器中有方法,可以在POST请求的正文中接收数据。
Here is method. 这是方法。
// POST: api/StartWorkingDays
[ResponseType(typeof(StartWorkingDay))]
public IHttpActionResult PostStartWorkingDay(StartWorkingDay startWorkingDay)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.StartWorkingDays.Add(startWorkingDay);
db.SaveChanges();
return Json(new { Result = "Success", Message = "Saved Successfully"});
//return CreatedAtRoute("DefaultApi", new { id = startWorkingDay.Id }, startWorkingDay);
}
How I can see body of request that I receive? 我如何看待收到的请求书?
Thank's for help. 感谢帮助。
[HttpPost]
public HttpResponseMessage PostStartWorkingDay([FromBody] StartWorkingDay startWorkingDay)
{
//here above startWorkingDay is body your mobile developer will send
//you and data can be viewed while debugging ,
//tell mobile developer to set content-type header should be JSON.
return Request.CreateResponse(HttpStatusCode.Created, "Success");
}
Why your return type is Json ? 为什么您的返回类型为Json? you should use HttpResponse .
您应该使用HttpResponse。 I believe you are using Web api 2 .
我相信您正在使用Web api 2。 With Attribute routing and if you want to send response in json format then remove Xml formatter from WebApiConfig.cs file inside App_Start folder
使用属性路由,如果您想以json格式发送响应,则从App_Start文件夹内的WebApiConfig.cs文件中删除Xml格式化程序
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.