Skip to main content

ASP.NET Core API Best Practices

References

https://medium.com/@jeslurrahman/14-best-practices-for-designing-restful-apis-net-core-web-api-1f34d6b8303e

https://dev.to/yogini16/10-bad-practices-to-avoid-in-aspnet-core-api-controllers-2pba

Best Practices

  • In general, try to have one Controller for each Model.

  • If controllers start getting too large, we can consider breaking them up.

  • Use plural nouns for collections of objects, i.e. for a model called "Node" the controller would be "NodeController" and the API endpoint to GET a collection of model instances would be /api/nodes.

  • Use attribute routing, i.e.:

    [Route("clients/{clientId}/orders")]
    public IEnumerable GetOrdersByClient(int clientId)
    {
    //TO DO
    }
  • Use try/catch and return proper status codes upon error, i.e. Bad Request, Not Found, ...