You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/tutorials/first-mongo-app.md
+27-27
Original file line number
Diff line number
Diff line change
@@ -263,6 +263,33 @@ The preceding web API controller:
263
263
* Contains action methods to support GET, POST, PUT, and DELETE HTTP requests.
264
264
* Calls <xref:Microsoft.AspNetCore.Mvc.ControllerBase.CreatedAtAction%2A> in the `Create` action method to return an [HTTP 201](https://www.rfc-editor.org/rfc/rfc9110#status.201) response. Status code 201 is the standard response for an HTTP POST method that creates a new resource on the server. `CreatedAtAction` also adds a `Location` header to the response. The `Location` header specifies the URI of the newly created book.
265
265
266
+
## Configure JSON serialization options
267
+
268
+
There are two details to change about the JSON responses returned in the [Test the web API](#test-the-web-api) section:
269
+
270
+
* The property names' default camel casing should be changed to match the Pascal casing of the CLR object's property names.
271
+
* The `bookName` property should be returned as `Name`.
272
+
273
+
To satisfy the preceding requirements, make the following changes:
274
+
275
+
1. In `Program.cs`, chain the following highlighted code on to the `AddControllers` method call:
With the preceding change, property names in the web API's serialized JSON response match their corresponding property names in the CLR object type. For example, the `Book` class's `Author` property serializes as `Author` instead of `author`.
280
+
281
+
1. In `Models/Book.cs`, annotate the `BookName` property with the [`[JsonPropertyName]`](xref:System.Text.Json.Serialization.JsonPropertyNameAttribute) attribute:
With the preceding change, property names in the web API's serialized JSON response match their corresponding property names in the CLR object type. For example, the `Book` class's `Author` property serializes as `Author` instead of `author`.
510
-
511
-
1. In `Models/Book.cs`, annotate the `BookName` property with the [`[JsonPropertyName]`](xref:System.Text.Json.Serialization.JsonPropertyNameAttribute) attribute:
0 commit comments