Creating a REST API with ASP.NET 7 is as easy as eating a banana. But adhering to the standard URI naming convention for REST APIs can be a challenge for developers.
Because I got numerous upvote and notification from my Stack Overflow answer which I posted in 2018, addressing this issue.
Since then, Swagger is most popular tool to design the API and is already integrated into the Microsoft ASP.NET API project template, leading many developers to use Swagger and face issues with URI naming conventions. Swagger will throw an error if two URIs are ambiguous.
In the REST API standard URI naming convention, all URLs must follow a plural object or noun pattern. Method verbs are not allowed, and HTTP verbs are used to differentiate the URLs. The following HTTP verbs are useful for CRUD database APIs:
HttpGet
Retrieves something from the server. This is a read-only method.
HttpPost
Creates a new resource at the specified URI. This can be used to create a record for database CRUD functions.
HttpPut
Suitable for creating or updating the resource at the specified URI. For example, updating the record or changing the record status for database CRUD functions.
HttpPatch
Used for partial updates to a record, such as a soft delete status change of a record.
HttpDelete
Deletes the record from database.
Here are some helpful articles I’ve read on the topic:
- Routing in ASP.NET Web API | Microsoft Learn
- Use web API conventions | Microsoft Learn
- Web API design best practices — Azure Architecture Center | Microsoft Learn
- REST API: Best Practices, Concepts, Structure, and Benefits | AltexSoft
- REST API — URL Naming Conventions (restfulapi.net)
- REST API Naming Conventions and Best Practices | by Nadin Pethiyagoda | Medium