简体   繁体   中英

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.

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 ? you should use HttpResponse . I believe you are using 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM