CS296N Web Development 2: ASP.NET
Brian Bird

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

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


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.


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.


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
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:


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


  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.


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:
  3. Enter the URL of the web API; for example:
  4. Select Headers and enter the header. If your request has a JSON body, then enter:
  5. Select Body and enter the request body; for example:
       "title": "Tom Sawyer",
       "date": "1/1/1876",
       "author": "Mark Twain",
       "birthdate": "11/30/1835"


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