简体   繁体   English

在ASP.NET Web Api中获取请求正文

[英]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.

相关问题 ASP.NET-GET Web请求API - ASP.NET - GET Web Request API Why Fetch Post request with JSON parameter in the body to ASP.NET Core 3.1 WEB API or MVC Controller does not get anything? - Why Fetch Post request with JSON parameter in the body to ASP.NET Core 3.1 WEB API or MVC Controller does not get anything? 如何在ASP.NET Web API中获取uri的请求对象? - How to get the request object for uri in ASP.NET Web API? 如何将ASP.NET Web API请求正文记录到Application Insights故障中? - How to log ASP.NET Web API request body into a Application Insights failure? ASP.NET MVC Web Api:服务器端的application / xml PUT请求主体为null - ASP.NET MVC Web Api: application/xml PUT request body is null on server side 如何在 ASP.NET Core 5 Web API 中绑定来自请求主体的简单类型 - How can I bind simple type coming from body of the request in ASP.NET Core 5 Web API 如何在生产环境中禁用 Asp.Net Core web API 中的 400 Bad request 的消息正文? - how to disable 400 Bad request's message body in Asp.Net Core web API in production environment? asp.net Web api:将请求正文值绑定到模型类 - asp.net web api : bind request body values to the model class 如何将 POST 请求正文中的有效 XML 提交到使用 XML 的 ASP.NET Core Web API? - How to submit valid XML in a POST request body to an ASP.NET Core Web API that consumes XML? 如何通过仅允许在 asp.net 核心 web Z8A35DA52ED1057271 的 Model 中分配的属性来验证请求正文 - How to Validate Request Body by allowing only attributes assigned in the Model of an asp.net core web api app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM