C Sharp - API Topics

My API’s are usually exposed using what I call controlling classes. These classes define different API methods.

Sample class

Here’s an example of a controlling class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using Microsoft.AspNetCore.Mvc;
using Checkbook.API.Base;
namespace Checkbook.API.Reconcile
{
// test with http://localhost:xxx/api/v1/reconciles
[Route("api/v1/[controller]")]
[ApiController]
public class ReconcilesController : BaseController
{
private readonly IReconcileService _reconcileService;

public ReconcilesController(IReconcileService reconcileService)
{
_reconcileService = reconcileService;
}

[HttpGet]
public async Task<IActionResult> GetAll()
{
IResult<List<ReconcileViewModel>> result = await _reconcileService.GetAllReconcilesAsync();
return Ok(result);
}

[HttpGet("{id}")]
public async Task<IActionResult> GetOne(long id)
{
var result = await _reconcileService.GetReconcileById(id);
return Ok(result);
}

[HttpGet("{bankAccountId}/{startDate}")]
public async Task<IActionResult> GetByBankAccountNdStartDate(long bankAccountId, DateOnly startDate)
{
var result = await _reconcileService.GetByBankAccountNdStartDate(bankAccountId, startDate);
return ReturnResult(result);
}

[HttpPost]
public async Task<IActionResult> Add(
[FromBody] IEnumerable<ReconcileViewModel> reconcileVm)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);

var result = await _reconcileService.AddReconcile(reconcileVm);
return Ok(result);
}

[HttpPut]
public async Task<IActionResult> Update([FromBody] ReconcileViewModel reconcileVm)
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
//return ReturnResult(_reconcileService.UpdateReconcile(reconcileVm));

var result = await _reconcileService.UpdateReconcile(reconcileVm);
return Ok(result);
}

[HttpDelete("{id}")]
public async Task<IActionResult> Delete(long id)
{
var result = await _reconcileService.DeleteReconcile(id);
return Ok(result);
}
}
}

Here are a few interesting commands

1
2
3
4
5
// test with http://localhost:xxx/api/v1/reconciles
[Route("api/v1/[controller]")]
[ApiController]
public class ReconcilesController : BaseController
{

The Route and ApiController statements are called Annotation’s. You add Annotations to classes, or methods as a way to add functionality to your code without adding the code itself.

Route defines the URL path to access methods in the class.

ApiController is used to tell the compiler that the class is an API controller. When you use the ApiController attribute, you are required to add the word Controller to the class name as a suffix.

‘public class ReconcilesController’ is the class name. The methods in this class can be reached with something like http://localhost:5000/api/v1/reconciles

1
2
3
4
5
6
private readonly IReconcileService _reconcileService;

public ReconcilesController(IReconcileService reconcileService)
{
_reconcileService = reconcileService;
}

Public ReconcilesController(…) is executed each time the class is instantiated. When the class is instantiated, a property called Dependency Injection will inject (IReconcileService reconcileService) a class which was created that implements the IReconcileService interface.

We store a reference of the class being passed in into the private variable _reconcileService.

1
2
3
4
5
6
[HttpGet]
public async Task<IActionResult> GetAll()
{
IResult<List<ReconcileViewModel>> result = await _reconcileService.GetAllReconcilesAsync();
return Ok(result);
}

This method returns records from the database. It does this by calling the GetAllReconcilesAsync() method in the class that implements the IReconcileService interface. it returns a List object loaded with ReconcileViewModel objects. This is wrapped in a IResult object..


C Sharp - API Topic - HTTP tags - discussion about HttpGet, HttpPost, HttpPut tags.
C Sharp - API Topic - Swagger tags - discussion about Summary, Param, SwaggerResponse, SwaggerResponseExample,
C Sharp - API Topic - Parameter tags - discussion about Summary, Param, SwaggerResponse, SwaggerResponseExample,

More information about API can be found here: