This guide provides a quick overview of setting up a basic ASP.NET Core Web API project.
- π Visual Studio 2022 Settings
- ποΈ SQL Server Object Explorer
- π¦ Installing Packages (Package Manager Console)
- βοΈ appsettings.json Configuration
- π Program.cs Settings
- ποΈ Creating a Controller
- π Enabling CORS (Program.cs)
When creating a new project in Visual Studio 2022, ensure the following settings are configured:
- Project Template: ASP.NET Core Web API
- Place solution and project in the same directory: Check this option.
- Framework: .NET 8.0
- Configure for HTTPS: Check this option.
- Enable OpenAPI support (Swagger): Check this option.
- Use controllers (MVC): Ensure this is the selected option.
To create the database, you can use the SQL Server Object Explorer.
-
Add New Query: Open a new query window connected to your SQL Server instance.
-
Execute the following SQL command to create your database:
CREATE DATABASE YourDatabaseName;
-
You can convert sql codes to different languages here
Open the Package Manager Console in Visual Studio and run the following commands to install the necessary Entity Framework Core packages:
Install-Package Microsoft.EntityFrameworkCore.SqlServer
Install-Package Microsoft.EntityFrameworkCore.Design
Install-Package Microsoft.EntityFrameworkCore.Tools
To generate the database context and model classes from your existing database, use the following command. Remember to replace the placeholders with your actual connection string, context name, and model output directory.
Scaffold-DbContext "_YourConnectionString_" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Context _YourDbContext_ -DataAnnotations
Configure your database connection string in the appsettings.json
file:
{
"ConnectionStrings": {
"DefaultConnection": "_YourConnectionString_"
}
}
Replace _YourConnectionString_
with your actual SQL Server connection string.
Add the database connection to your application's services in the Program.cs
file:
var builder = WebApplication.CreateBuilder(args);
// ... other service configurations ...
var connectionString = builder.Configuration
.GetConnectionString("DefaultConnection");
builder.Services
.AddDbContext<_YourDbContext_>
(opt => opt.UseSqlServer(connectionString));
// ... rest of the Program.cs ...
When creating a new controller:
- Model Class: YourModel (Replace with the name of your model class)
- DbContext Class: YourDbContext (Replace with the name of your DbContext class)
- Controller Name: _YourModel_sController (By default, it's the pluralized form of your model name)
To enable Cross-Origin Resource Sharing (CORS) for development purposes, add the following code to your Program.cs file:
var builder = WebApplication.CreateBuilder(args);
// ... other service configurations ...
builder.Services.AddCors(
options => options.AddDefaultPolicy(
builder => builder
.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()));
var app = builder.Build();
// This must be added before app.UseHttpsRedirection();
app.UseCors();
app.UseHttpsRedirection();
// ... rest of the app pipeline ...
app.Run();
Note
Allowing AnyOrigin, AnyHeader, and AnyMethod is generally not recommended for production environments due to security implications. You should configure CORS with specific origins, headers, and methods as needed for your application.