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
+210-17
Original file line number
Diff line number
Diff line change
@@ -265,41 +265,234 @@ The preceding web API controller:
265
265
266
266
## Test the web API
267
267
268
+
# [Visual Studio](#tab/visual-studio)
269
+
270
+
This tutorial uses [Endpoints Explorer and .http files](xref:test/http-files#use-endpoints-explorer) to test the API.
271
+
268
272
1. Build and run the app.
269
273
270
-
1. Navigate to `https://localhost:<port>/api/books`, where `<port>` is the automatically assigned port number for the app, to test the controller's parameterless `Get` action method. A JSON response similar to the following is displayed:
274
+
1. In **Endpoints Explorer**, right-click the first **GET** endpoint `/api/books`, and select **Generate request**.
275
+
276
+
The following content is added to the `BookStoreApi.http` file.
The port number should already be set to the port used by the app. For example, `https://localhost:56874`.
287
+
To find your port number, check the output window when you launch the app.
288
+
289
+
1. Select the **Send request** link above the new `GET` request line.
290
+
291
+
The GET request is sent to the app and the response is displayed in the **Response** pane.
292
+
293
+
1. The response body shows the JSON result containing the book entries similar to the following:
271
294
272
295
```json
273
296
[
274
297
{
275
-
"id": "61a6058e6c43f32854e51f51",
276
-
"bookName": "Design Patterns",
277
-
"price": 54.93,
278
-
"category": "Computers",
279
-
"author": "Ralph Johnson"
298
+
"Id": "61a6058e6c43f32854e51f51",
299
+
"Name": "Design Patterns",
300
+
"Price": 54.93,
301
+
"Category": "Computers",
302
+
"Author": "Ralph Johnson"
280
303
},
281
304
{
282
-
"id": "61a6058e6c43f32854e51f52",
283
-
"bookName": "Clean Code",
284
-
"price": 43.15,
285
-
"category": "Computers",
286
-
"author": "Robert C. Martin"
305
+
"Id": "61a6058e6c43f32854e51f52",
306
+
"Name": "Clean Code",
307
+
"Price": 43.15,
308
+
"Category": "Computers",
309
+
"Author": "Robert C. Martin"
287
310
}
288
311
]
289
312
```
290
313
291
-
1. Navigate to `https://localhost:<port>/api/books/{id here}` to test the controller's overloaded `Get` action method. A JSON response similar to the following is displayed:
314
+
1. To retrieve a single book, right-click the `/api/books/{id}, params (string id)`**GET** endpoint in the **Endpoints Explorer**, and select **Generate request**.
315
+
316
+
The following content is appended to the `BookStoreApi.http` file:
317
+
318
+
```
319
+
@id=string
320
+
GET {{BookStoreApi_HostAddress}}/api/books/{{id}}
321
+
322
+
###
323
+
```
324
+
325
+
1. Replace `id` variable with one of the IDs returned from the earlier request, for example:
326
+
327
+
```
328
+
@id="61a6058e6c43f32854e51f52"
329
+
GET {{BookStoreApi_HostAddress}}/api/books/{{id}}
330
+
331
+
###
332
+
```
333
+
334
+
1. Select the **Send request** link above the new `GET` request line.
335
+
336
+
The GET request is sent to the app and the response is displayed in the **Response** pane.
337
+
338
+
1. The response body shows JSON similar to the following:
292
339
293
340
```json
294
341
{
295
-
"id": "61a6058e6c43f32854e51f52",
296
-
"bookName": "Clean Code",
297
-
"price": 43.15,
298
-
"category": "Computers",
299
-
"author": "Robert C. Martin"
342
+
"Id": "61a6058e6c43f32854e51f52",
343
+
"Name": "Clean Code",
344
+
"Price": 43.15,
345
+
"Category": "Computers",
346
+
"Author": "Robert C. Martin"
300
347
}
301
348
```
302
349
350
+
1. To test the POST endpoint, right-click the `/api/books`**POST** endpoint and select **Generate request**.
351
+
352
+
The following content is added to the `BookStoreApi.http` file:
353
+
354
+
```
355
+
POST {{BookStoreApi_HostAddress}}/api/books
356
+
Content-Type: application/json
357
+
358
+
{
359
+
//Book
360
+
}
361
+
362
+
###
363
+
```
364
+
365
+
1. Replace the Book comment with a book object as the JSON request body:
366
+
367
+
```
368
+
POST {{BookStoreApi_HostAddress}}/api/books
369
+
Content-Type: application/json
370
+
371
+
{
372
+
"Name": "The Pragmatic Programmer",
373
+
"Price": 49.99,
374
+
"Category": "Computers",
375
+
"Author": "Andy Hunt"
376
+
}
377
+
378
+
###
379
+
```
380
+
381
+
1. Select the **Send request** link above the `POST` request line.
382
+
383
+
The POST request is sent to the app, and the response is displayed in the **Response** pane. The response should include the newly created book with its assigned ID.
384
+
385
+
1. Lastly, to delete a book, right-click the `/api/books/{id}, params (string id)`**DELETE** endpoint and select **Generate request**.
386
+
387
+
The following content is appended to the `BookStoreApi.http` file:
This tutorial uses the [OpenAPI specification (openapi.json) and Swagger UI](xref:tutorials/web-api-help-pages-using-swagger) to test the API.
406
+
407
+
1. Install Swagger UI by running the following command:
408
+
409
+
```dotnetcli
410
+
dotnet add package NSwag.AspNetCore
411
+
```
412
+
413
+
The previous command adds the [NSwag.AspNetCore](https://www.nuget.org/packages/NSwag.AspNetCore/) package, which contains tools to generate Swagger documents and UI.
414
+
Because our project is using OpenAPI, we only use the NSwag package to generate the Swagger UI.
415
+
416
+
1. Configure Swagger middleware
417
+
418
+
In `Program.cs`, add the following highlighted code:
The previous code enables the Swagger middleware for serving the generated JSON document using the Swagger UI. Swagger is only enabled in a development environment. Enabling Swagger in a production environment could expose potentially sensitive details about the API's structure and implementation.
423
+
424
+
The app uses the OpenAPI document generated by OpenApi, located at `/openapi/v1.json`, to generate the UI.
425
+
View the generated OpenAPI specification for the `WeatherForecast` API while the project is running by navigating to `https://localhost:<port>/openapi/v1.json` in your browser.
426
+
427
+
The OpenAPI specification is a document in JSON format that describes the structure and capabilities of your API, including endpoints, request/response formats, parameters, and more. It's essentially a blueprint of your API that can be used by various tools to understand and interact with your API.
428
+
429
+
1. Build and run the app.
430
+
431
+
1. Navigate to `https://localhost:<port>/swagger` in your browser. Swagger provides a UI to test all the API endpoints based on the OpenAPI document.
432
+
433
+
1. Expand the **GET /api/books** endpoint and click the **Try it out** button.
434
+
435
+
1. Click the **Execute** button to send the request to the API.
436
+
437
+
1. The **Response body** section displays a JSON array with books similar to the following:
438
+
439
+
```json
440
+
[
441
+
{
442
+
"Id": "61a6058e6c43f32854e51f51",
443
+
"Name": "Design Patterns",
444
+
"Price": 54.93,
445
+
"Category": "Computers",
446
+
"Author": "Ralph Johnson"
447
+
},
448
+
{
449
+
"Id": "61a6058e6c43f32854e51f52",
450
+
"Name": "Clean Code",
451
+
"Price": 43.15,
452
+
"Category": "Computers",
453
+
"Author": "Robert C. Martin"
454
+
}
455
+
]
456
+
```
457
+
458
+
1. Next, expand the **GET /api/books/{id}** endpoint and click **Try it out**.
459
+
460
+
1. Enter one of the book IDs from the previous response in the **id** field, then click **Execute**.
461
+
462
+
1. The **Response body** section displays the JSON object for the specified book. For example, the result for the ID `61a6058e6c43f32854e51f52` is similar to the following:
463
+
464
+
```json
465
+
{
466
+
"Id": "61a6058e6c43f32854e51f52",
467
+
"Name": "Clean Code",
468
+
"Price": 43.15,
469
+
"Category": "Computers",
470
+
"Author": "Robert C. Martin"
471
+
}
472
+
```
473
+
474
+
1. To test creating a new book, expand the **POST /api/books** endpoint and click **Try it out**.
475
+
476
+
1. Replace the default request body with a new book object:
477
+
478
+
```json
479
+
{
480
+
"Name": "The Pragmatic Programmer",
481
+
"Price": 49.99,
482
+
"Category": "Computers",
483
+
"Author": "Andy Hunt"
484
+
}
485
+
```
486
+
487
+
1. Click **Execute** to send the request.
488
+
489
+
1. The response should have a status code of 201 (Created) and include the newly created book with its assigned ID in the response body.
490
+
491
+
1. Lastly, to delete a book record, expand the **DELETE /api/books/{id}** endpoint, click **Try it out**, and enter one of the book IDs from the previous response in the **id** field. Click **Execute** to send the request.
492
+
493
+
1. The response should have a status code of 204 (No Content), indicating that the book was successfully deleted.
494
+
---
495
+
303
496
## Configure JSON serialization options
304
497
305
498
There are two details to change about the JSON responses returned in the [Test the web API](#test-the-web-api) section:
0 commit comments