CS296N Web Development 2: ASP.NET
Brian Bird

Topic: Implementing a REST Web API in ASP.NET Core

Topics
1. Intro to course and validation
6. Load Testing and Performance
2. Identity 7. Creating a Web API
3. Authentication and Authorization 8. Consuming a Web Service, Using Async / Await
4. Web Security
9. Docker Containers
5. Publishing to a Production Server 10.  Term project

Introduction

Last time we talked about web services in general. Today, we'll look at how to implement one using ASP.NET Core. The approach we're using follows the example in our textbook (Freeman, 2017). It's a little different from the approach shown in the Microsoft Tutorial.

Implementation

You will be writing action methods in each of your API controllers to handle any of the HTTP request verbs that are important for your application.

Controller

An API controller should inherit from ControllerBase and have the [ApiController] attribute.

Controller Methods

There will be one method for each HTTP verb. You can write the method using an ActionMethod or IActionMethod return type, but just return a POCO object and MVC will figure out the correct format for the returned data--including the HTTP status code. In addition, you can explicitly declare the status code.

Each action method should have the route annotation below so that the method names don't need to be included in the URL
[Route("api/[controller]")]
URL will be: http://localhost:5000/api/Book/

Status Codes

Each of the methods returns an IActionResult which allows the use of StatusCode helper methods. HTTP Status codes, along with the available status code helper methods are shown below:

Examples

  HTTP requests and a controller methods are shown below for different HTTP request methods (verbs).

Notes

  1. In ASP.NET Core version 2.0, use of the [ApiController] annotation was deprecated, but in 2.1 and later it is again required on a controller class that will provide API endpoints.
  2. Your controller can be a subclass of Controller, but it is better to make it a subclass of ControllerBase (Controller is a sub-class of ControllerBase) since Controller contains View related members that you don't need.

Testing

One way to test a web API is to use a tool like Postman. This will let you write an HTTP request, send it to an API endpoint, and view the response. You can save the request for use again later.

Steps to create a request in Postman:

  1. Click + to create a new request tab.
  2. Select a request method (verb) from the drop-down list; for example:
    POST
  3. Enter the URL of the web API; for example:
    http://localhost:5000/api/Book
  4. Select Headers and enter the header. If your request has a JSON body, then enter:
    Content-Type:application/json
  5. Select Body and enter the request body; for example:
    {
       "title": "Tom Sawyer",
       "date": "1/1/1876",
       "author": "Mark Twain",
       "birthdate": "11/30/1835"
    }


Examples

  1. Source code on GitHub: ProfBird/BookInfo-Core-2-WebAPI
  2. Web service running online: BookInfo Service

References

Creative Commons License
These ASP.NET Core MVC Lecture Notes are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License. Written by Brian Bird, winter 2019, revised by Brian Bird winter 2020.