Create the Project
Open rider and select New Solution
Enter CrudApp as the solution name
Enter CrudAppAPI as the project name
Choose the solution directory
Uncheck put solution and project in the same directory
Choose whether to create a Git repository
Select template Web API and press create
Open the Program.cs file and delete the comments, example endpoint along with its variable and record. You should then be left with a clean empty project
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseHttpsRedirection();
app.Run();
We will need to add nuget packages to use the methods required to connect to our SQL database. We will also use Dapper to simplify our database access and FluentValidation to validate incoming data.
Open the NuGet package manager and search for the following and add them to the project:
Dapper FluentValidation FluentValidation.AspNetCore FluentValidation.DependencyInjectionExtensions Microsoft.Data.SqlClient
To connect to our database, we will need to add a connection string with the required details.
Add the connection string to the appsettings.Development.json file
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost,1433;Database=CrudApp;User Id=sa;Password=Password123;TrustServerCertificate=True;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}