官术网_书友最值得收藏!

Message body

HTTP allows the sender to send a message body with the request. This is useful not only for sending complex structures but also for hiding information since unlike the URL, the message body can be encrypted (for example, when using HTTP Secure (HTTPS). Not every HTTP request type can send a message body; for example, adding a message body to a GET request is usually not supported by server frameworks, and here, ASP.NET Core is no exception.

When a request with a message body is sent to a server, it should contain the Content-Type header with the message body type as one of the standard MIME types. ASP.NET Core uses the content type value to determine which formatter to use in order to deserialize the message body to a method parameter. The default formatter is JsonInputFormatterwhich is based on the popular package Json.NET, but other formatters exist for other types.

ASP.NET Core uses the [FromBody] attribute to mark the method parameter that should be bound to the request's message body.

Here is how you can add an API that will let users add a new product to the GiveNTake application, and then test it:

  1. Create a new folder is your solution with the name Model.
  2. Create a new class in the Model folder, called NewRequestDTO, and paste the following code into your new class:
public class NewProductDTO
{
public string Name { get; set; }
public string Category { get; set; }
public string Subcategory { get; set; }
}

The NewProductDTO is a POCO class that will be filled with the values supplied in the request body. Data Transfer Object (DTO) is a common name for a type that you expose from your APIs. We usually add the DTO postfix to differentiate classes you use only in the API layer and classes that are used in the business logic:

  1. Add the AddNewProduct method to your ProductsController. For now, this method will receive the NewProductDTO and return it:
[HttpPost("")]
public ActionResult<NewProductDTO> AddNewProduct([FromBody] NewProductDTO newProduct)
{
return Ok(newProduct);
}
We used the [HttpPost] attribute with an empty string. This means that the AddNewProduct method will be invoked by POST requests that are sent to the Controller route, in this case,  /api/products. The method received a single parameter of the NewProductDTO type, and the [FromBody] attribute signals the ASP.NET Core pipeline to bind this parameter to the request body. Don't worry if you don't understand what the meaning of the ActionResult return type and the use of the Ok(...) method is yet — we will talk about them in the next section.
  1. Build and run the application.
  2. Open Postman and create a new POST request to the following URL: /api/products.
  3. Select the Body inner tab and the raw option.
  4. Change the content type from Text to JSON (application/json). Fill the request body with the following JSON:
{
"Name" : "New Product",
"Category" : "Appliances",
"Subcategory" : "Microwaves"
}
  1. Postman should now look like this:

  1. Click on the Send button, and look at the result at the bottom of the Postman window. It should reflect the same values that you sent in the request, like this:

主站蜘蛛池模板: 榆林市| 正镶白旗| 财经| 文安县| 朔州市| 莒南县| 彭阳县| 濮阳县| 宁远县| 郸城县| 博白县| 沁源县| 琼海市| 增城市| 左云县| 策勒县| 凤庆县| 即墨市| 益阳市| 弥勒县| 静宁县| 农安县| 泰和县| 新邵县| 浦北县| 旌德县| 界首市| 商水县| 东源县| 竹山县| 晴隆县| 扶沟县| 舒兰市| 和硕县| 南江县| 即墨市| 隆德县| 鹤壁市| 庆云县| 台北市| 新兴县|