This project is a .NET WebAPI project that demonstrates how to use resources (resx) files to provide messages for validations and errors. The objective is to centralize the error and validation messages in resource files, standardize the messages and making it easier to manage.
I have written an article explaining the implementation details of this project. You can find the article here.
- .NET 7 SDK
- Visual Studio Code (or any other preferred code editor)
Clone this repository to your local machine using the following command:
git clone https://github.com/JVidalN/dotnet-standardizing-messages-with-resource-resx-files.git
- Open the project in Visual Studio Code (or your preferred code editor).
- Restore the dependencies and build the project:
dotnet restore
dotnet build
dotnet run
The API will start running at https://localhost:7140
(or http://localhost:5041
for HTTP) by default.
-
Create the .NET solution file using the following command:
dotnet new sln -n ResourceResx
-
Create the project:
dotnet new webapi -n WebApiResource
-
Add the created project to the solution:
dotnet sln add WebApiResource/WebApiResource.csproj
The project contains two resource files located in the Resources
folder:
-
ErrorMessages.resx
: Contains error messages with HTTP status codes as keys and corresponding error messages as values. This allows us to easily return standard error messages for specific HTTP status codes. -
ValidationMessages.resx
: Contains validation messages with keys such as "Required", "Email", "Length", etc., which are used for annotating the model properties in theUser
model.
The project demonstrates how to use the resource files to provide error and validation messages in the API.
To understand in more detail how it works and the idea, read the article I made. Article link in the Article section above.
Contributions to this project are welcome! If you find any issues or have suggestions for improvements, feel free to open an issue or submit a pull request.