ASP.NET Core 配置系列一

发布时间 2024-01-01 13:06:20作者: bingbing-gui

ASP.NET Core 3

1 .csproj 

2 Program.cs

3 appsettings.json

ASP.NET Core 的,ASP.NET Core 

1  使

使

1.1  ControllersView&Models

1.2  appsettings.json

1.3  layoutsscripts 

Visual Studio ASP.NET Core Empty

 2 .csproj

.csproj ASP.NET Core 件,SDKASP.NET Core 等,.csproj

.NET 7 .csproj :

<Project Sdk="Microsoft.NET.Sdk.Web">  
  <PropertyGroup>    
    <TargetFramework>net7.0</TargetFramework>    
    <Nullable>enable</Nullable>    
    <ImplicitUsings>enable</ImplicitUsings>  
  </PropertyGroup>
</Project>

使csprojNuGetASP.NET CorecsprojVisual StudiocsprojVisual Studio

Json.NET .NETJson使JSON, .csprojItemGroupPackageReference

 csprojVSNuGet

2.1 csproj



Project Sdk使Microsoft.NET.Sdk.Web
PropertyGroup Property
TargetFramework .NET 使
ItemGroup Item
PackageReference NuGet-Microsoft.AspNetCore.AllASP.NET CoreMVC访

2.2 使NuGet Package Manager

NuGet Package Manager vsNuGet Tools  NuGet Package Manager  Manage NuGet Packages (), BrowseListInstall

Json.NET

 Package Manager ConsolepackageVSTools->NuGet Package Manager->Packages Manager ConsoleJSON.NET

Install-Package Newtonsoft.Json

3 .NET Core Kestrel Server 

KestrelWeb宿ASP.NET (WindowsLinuxmacOS)ASP.NET Core使IIS使使Kestrel使"IIS,NginxApache"HTTPKestrel

Visual Studio 使Kestrel绿"https"使宿IIS Express

使IIS express Visual Studio绿IIS Express

4 ASP.NET Core Program 

 

ProgramASP.NET Core .NET Core

Program

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();

使Web宿Build()

app.MapGet("/",()=>"Hello World") "Hello World"

 4.1 

Program.cs便使Programe

AddControllersWithViews > ControllersAPI务,AddRazorPages > Razor Pages 务,AddDbContext >DBContext使Entity Framework Core AddDefaultIdentity >  Identity -AddControllersWithViewsAddRazorPages

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();

Program.cs ASP.NET CoreHTTP

UseDeveloperExceptionPage -> , 

UseExceptionHandler > 使UseHsts > HSTSHSTS  HTTP HTTP Strict Transport SecurityUseHttpsRedirection > httpshttphttpsUseStaticFiles > UseRouting > UseAuthorization > Authorization UseAuthentication >  Authentication UseEndpoints > MapControllerRoute > urlactions

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllersWithViews();
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    // The default HSTS value is 30 days.
    app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();

ASP.NET CorecsprojKestrel ServerProgram

址 :https://github.com/bingbing-gui/Asp.Net-Core-Skill/tree/master/Fundamentals/AspNetCore.Configuration/AspNetCore.Configuration