Rilasciato · in miglioramento
Guida a ASP.NET Core · 1/6
Per ora questo capitolo è disponibile solo in inglese.
This chapter installs the .NET SDK, creates a web project with dotnet new, runs it with dotnet run and dotnet watch, and sets up an editor and the HTTPS development certificate.
ASP.NET Core is not a separate download: it ships with the .NET SDK. One SDK install gives you the C# compiler, the dotnet command-line tool, the ASP.NET Core runtime and the project templates.
dotnet-install.sh script.Open a new terminal and check the installation:
dotnet --version # SDK used by default in this folder
dotnet --list-sdks # every installed SDK
dotnet --list-runtimes # look for Microsoft.AspNetCore.App.NET alternates long-term support (LTS) and standard-term support (STS) releases. For a new project, pick the latest LTS or the latest stable release.
dotnet new scaffolds projects from templates. The ones you will use most for web work:
| Template | Creates |
|---|---|
web | An empty app with a single minimal API endpoint |
webapi | A Web API with a sample endpoint and an OpenAPI document |
webapi --use-controllers | A Web API based on controllers |
mvc | A Model-View-Controller web app |
razor | A Razor Pages web app |
blazor | A Blazor web app |
# Empty web app
dotnet new web -o HelloWeb
# Web API using minimal APIs
dotnet new webapi -o TodoApi
cd TodoApi
# All installed templates
dotnet new list-o sets the output folder, which also becomes the project name. The Program.cs of the web template is only a few lines:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello World!");
app.Run();WebApplication.CreateBuilder prepares configuration, logging, dependency injection and the Kestrel web server with sensible defaults. registers a handler for GET requests to , and starts the server and blocks until the app shuts down. There is no method because the file uses C# top-level statements, and common namespaces come from implicit usings.
MapGet/app.Run()Maindotnet run # build, then run
dotnet run --launch-profile https # use the https profile
dotnet watch # run and watch for file changesdotnet run uses the first profile in Properties/launchSettings.json and prints the listening address, such as Now listening on: http://localhost:5123. Templates choose a random port per project, so read the address from the console.
dotnet watch applies edits to the running app with Hot Reload. Changes such as editing a method body are applied without a restart; for edits that cannot be applied in place, it offers to restart the app. Press Ctrl+R to force a restart.
Try the app from another terminal. The webapi template also creates a TodoApi.http file that Visual Studio and VS Code can send requests from.
# use the port printed by dotnet run
curl http://localhost:5123/weatherforecastThe https profile needs a trusted local certificate. The SDK creates and trusts one for you:
dotnet dev-certs https --trust
dotnet dev-certs https --checkOn Windows and macOS this adds the certificate to the system trust store. On Linux, trusting it may need extra, distribution-specific steps.
Whatever the editor, the dotnet CLI stays the source of truth: projects build and run the same way in the terminal, in the IDE and in CI.
dotnet new web or dotnet new webapi.dotnet run and iterate with dotnet watch.dotnet dev-certs https --trust to use HTTPS locally.
0 commenti
Accedi · Accedi per lasciare un commento.
Scrivi tu il primo commento.