How to quickly make an API controller for a data object in C#

Posted by:

|

On:

|

If you need a quick API controller, first make sure you are using Microsoft.AspNetCore.Mvc, then inherit the base class. I am going to do it for locations here:

You’ll note this part:

[Route("api/[controller]")]
[ApiController]

The first part defines the route for your api. [controller] will be replaced with the name of the class -Controller. In other words, to reach this api, we would go to api/location. The second part, ApiController, is just an attribute that enables API features like attribute routing.

From here you can write in your HTTP methods that you need for you api controller. It’s really that easy.