/TechBlogCore

A blog back-end based on ASP.NET Core Rest API + EF Core + MariaDB + JWT Authorization

Primary LanguageC#MIT LicenseMIT


简介 | Introduction

简约时尚风格博客后端,采用 ASP.NET Core 7 Rest API + EF Core + MariaDB + JWT 鉴权

需要项目预览,请移步 这里

A blog back-end based on ASP.NET Core 7 Rest API + EF Core + MariaDB + JWT Authorization

If you want a preview please visit here

Project Setup

Install Database

You can install either MySQL or MariaDB:

sudo yum install mariadb-server

If you want to change database to SQL Server,

change the nuget package from Pomelo.EntityFrameworkCore.Mysql to Microsoft.EntityFrameworkCore.SqlServer:

dotnet remove package Pomelo.EntityFrameworkCore.Mysql
dotnet add package Microsoft.EntityFrameworkCore.SqlServer --version 7.0.2

And configure as it should be.

Configure Launch Settings

Change IP address to your local network address in Properties/launchSettings.json:

"applicationUrl": "http://192.168.2.233:7084"

Configure CORS Options

Change the IP address and port depends on your local network environment

builder.Services.AddCors(options =>
{
    options.AddPolicy(name: "_myAllowSpecificOrigins",
                    policy =>
                    {
                        policy.WithOrigins("http://127.0.0.1:5173", "http://localhost:5173",
                        "http://127.0.0.1:5174", "http://localhost:5174", "http://192.168.2.233:5173")
                            .WithExposedHeaders("X-Pagination")
                            .SetIsOriginAllowed(x => _ = true)
                            .AllowAnyMethod()
                            .AllowAnyHeader()
                            .AllowCredentials();
                    }
    );
});

Specify Database Version for EF Core

For other Linux distro/Windows, you can install any version of MariaDB/MySQL

var sqlVersion = new MariaDbServerVersion(new Version(5, 5, 68)); //change this to your database version

Configure Database ConnectionString

Configure your database connectionstring in appsettings.Development.json for development, or appsettings.json for production:

"ConnectionStrings": {
  "DefaultConnection": "server=localhost;port=3306;database=techblog;user=root;password=pswd"
}

Update Database

cd TechBlogCore.RestApi
dotnet ef database update

Compile and Run for Development

dotnet run

Compile and Release for Production

dotnet publish