Skip to content

BevizLaszlo/REST-API-with-ASP.NET-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 

Repository files navigation

ASP.NET Core Web API - Quick Start Guide

This guide provides a quick overview of setting up a basic ASP.NET Core Web API project.

Table of Contents

πŸ“Œ Visual Studio 2022 Settings

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.

πŸ—„οΈ SQL Server Object Explorer

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

πŸ“¦ Installing Packages (Package Manager Console)

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

βš™οΈ appsettings.json Configuration

Configure your database connection string in the appsettings.json file:

{
  "ConnectionStrings": {
    "DefaultConnection": "_YourConnectionString_"
  }
}

Replace _YourConnectionString_ with your actual SQL Server connection string.

πŸ“ Program.cs Settings

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 ...

πŸŽ›οΈ Creating a Controller

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)

πŸ”“ Enabling CORS (Program.cs)

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.

About

REST API with ASP.NET tutorial

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published