已發布·持續改進
ASP.NET Core 指南 · 2/6
本章目前僅提供英文版。
This chapter walks through the files of a new project, explains how configuration and environments work, and shows common ways to organize code as the app grows.
dotnet new webapi -o TodoApi produces:
TodoApi/
├── Program.cs
├── TodoApi.csproj
├── TodoApi.http
├── appsettings.json
├── appsettings.Development.json
├── Properties/
│ └── launchSettings.json
├── bin/
└── obj/| File | Purpose |
|---|---|
Program.cs | Entry point: registers services and builds the request pipeline |
TodoApi.csproj | Project file: SDK, target framework, NuGet packages |
appsettings*.json | Configuration, with per-environment overrides |
Properties/launchSettings.json | Local launch profiles for dotnet run and IDEs |
TodoApi.http | Sample HTTP requests to send from the editor |
bin/, obj/ | Build output; keep them out of source control |
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>Microsoft.NET.Sdk.Web makes this an ASP.NET Core project: it references the shared ASP.NET Core framework and adds web-specific implicit usings. TargetFramework follows the SDK that created the project. Packages are added with the CLI, which writes a PackageReference into this file:
dotnet add package Microsoft.EntityFrameworkCore.Sqlite
dotnet list packageEvery Program.cs has the same shape: register services on the builder, then build the app and describe the pipeline. Services must be registered before Build(); after that the container is read-only.
var builder = WebApplication.CreateBuilder(args);
// 1. Services for the dependency injection container
builder.Services.AddOpenApi();
var app = builder.Build();
// 2. Middleware pipeline and endpoints
if (app.Environment.IsDevelopment())
{
app.MapOpenApi();
}
app.UseHttpsRedirection();
app.MapGet("/health", () => "ok");
app.Run();{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"Default": "Data Source=todo.db"
},
"Todo": {
"PageSize": 20
}
}The environment name comes from ASPNETCORE_ENVIRONMENT (or DOTNET_ENVIRONMENT). Typical values are Development, Staging and Production; when nothing is set, the app runs as Production. appsettings.{Environment}.json is layered on top of appsettings.json, so it only needs the keys that differ.
The default builder reads configuration in this order, and later sources win:
appsettings.jsonappsettings.{Environment}.jsonDevelopment only)Nested keys use : in code and on the command line, and __ (double underscore) in environment variables:
export ASPNETCORE_ENVIRONMENT=Staging
export Todo__PageSize=50
dotnet run
# or pass the value as an argument to the app
dotnet run -- --Todo:PageSize=50Keep secrets out of appsettings.json. During development, the Secret Manager stores them outside the project folder:
dotnet user-secrets init
dotnet user-secrets set "ConnectionStrings:Default" "Host=localhost;Database=todo;Username=dev;Password=dev"Properties/launchSettings.json holds the local profiles (URLs and environment variables such as ASPNETCORE_ENVIRONMENT=Development). It is used only by dotnet run and IDEs and is not part of the published output, so production settings belong in environment variables or configuration files.
ASP.NET Core does not enforce a folder layout. Common conventions:
Endpoints/ or Controllers/: the HTTP surface, grouped by featureServices/: business logic registered in DIData/: the EF Core DbContext and its Migrations/Models/ or Contracts/: entities and request/response typeswwwroot/: static files served by the static file middlewareLarger solutions put the app under src/ and tests under tests/, tied together by a solution file (dotnet new sln, then dotnet sln add). Feature endpoints are often moved out of Program.cs into extension methods:
// Endpoints/TodoEndpoints.cs
public static class TodoEndpoints
{
public static RouteGroupBuilder MapTodoEndpoints(this IEndpointRouteBuilder app)
{
var group = app.MapGroup("/todos");
group.MapGet("/", () => Array.Empty<string>());
return group;
}
}
// Program.cs
app.MapTodoEndpoints();Program.cs registers services, then builds the middleware pipeline and endpoints..csproj file uses the Web SDK; packages come from dotnet add package.appsettings.{Environment}.json.Program.cs short.
0 則留言
登入 · 登入後即可留言。
來留下第一則留言吧。