Skip to content

Commit 1275c90

Browse files
authored
Merge pull request #47930 from MicrosoftDocs/NEW-build-first-aspnet-web-app
[NEW MODULE] (merge release branch to main): build-your-first-aspnet-core-web-app
2 parents 98e4599 + 9de4c59 commit 1275c90

21 files changed

+601
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.build-your-first-aspnet-core-web-app.introduction
3+
title: Introduction
4+
metadata:
5+
title: Introduction
6+
description: Build your first ASP.NET Core web app.
7+
ms.date: 9/9/2024
8+
author: wadepickett
9+
ms.author: wpickett
10+
ms.topic: unit
11+
12+
durationInMinutes: 1
13+
content: |
14+
[!include[](includes/1-introduction.md)]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.build-your-first-aspnet-core-web-app.learning-content
3+
title: The ASP.NET Core project templates
4+
metadata:
5+
title: learning-content
6+
description: Review available ASP.NET Core project templates that can get you productive quickly.
7+
ms.date: 9/9/2024
8+
author: wadepickett
9+
ms.author: wpickett
10+
ms.topic: unit
11+
12+
durationInMinutes: 2
13+
content: |
14+
[!include[](includes/2-learning-content.md)]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.build-your-first-aspnet-core-web-app.exercise-create-project
3+
title: Exercise - Create an ASP.NET Core web app project from a template
4+
metadata:
5+
title: Exercise - Create an ASP.NET Core web app project from a template
6+
description: Create your first ASP.NET Core web app and examine the project structure.
7+
ms.date: 9/9/2024
8+
author: wadepickett
9+
ms.author: wpickett
10+
ms.topic: unit
11+
zone_pivot_groups: vscode-shell
12+
durationInMinutes: 2
13+
content: |
14+
[!include[](includes/3-exercise-create-project.md)]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.build-your-first-aspnet-core-web-app.exercise-run-project
3+
title: Exercise - Run your web app locally
4+
metadata:
5+
title: Exercise - Run your web app locally
6+
description: Run your web app locally and review how the web app is served.
7+
ms.date: 9/9/2024
8+
author: wadepickett
9+
ms.author: wpickett
10+
ms.topic: unit
11+
zone_pivot_groups: vscode-shell
12+
durationInMinutes: 2
13+
content: |
14+
[!include[](includes/4-exercise-run-project.md)]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.build-your-first-aspnet-core-web-app.exercise-make-code-changes
3+
title: Exercise - Make code changes
4+
metadata:
5+
title: Exercise - Make code changes
6+
description: Make code changes during local development.
7+
ms.date: 9/9/2024
8+
author: wadepickett
9+
ms.author: wpickett
10+
ms.topic: unit
11+
zone_pivot_groups: vscode-shell
12+
durationInMinutes: 2
13+
content: |
14+
[!include[](includes/5-exercise-make-code-changes.md)]
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.build-your-first-aspnet-core-web-app.knowledge-check
3+
title: Knowledge check
4+
metadata:
5+
title: Knowledge check
6+
description: Check your knowledge about creating and running an ASP.NET Core app using the .NET CLI.
7+
ms.date: 9/9/2024
8+
author: wadepickett
9+
ms.author: wpickett
10+
ms.topic: unit
11+
durationInMinutes: 3
12+
content: |
13+
quiz:
14+
questions:
15+
- content: "What do you need to install *at a minimum* to create and run ASP.NET Core apps?"
16+
choices:
17+
- content: "Visual Studio"
18+
isCorrect: false
19+
explanation: "Visual Studio is a great tool for building ASP.NET Core apps, but only the .NET SDK is required."
20+
- content: "Node.js"
21+
isCorrect: false
22+
explanation: "Create and build ASP.NET Core apps using the .NET SDK."
23+
- content: "Visual Studio Code"
24+
isCorrect: false
25+
explanation: "Visual Studio Code with the C# Dev Kit is a great editor for working with ASP.NET Core apps, but only the .NET SDK is required."
26+
- content: ".NET SDK"
27+
isCorrect: true
28+
explanation: "Only the .NET SDK is needed to create, build, and run ASP.NET Core apps. Tools like Visual Studio and Visual Studio Code can then help you be much more productive."
29+
- content: "What are ASP.NET Core project templates used for?"
30+
choices:
31+
- content: "Only for creating empty web projects."
32+
isCorrect: false
33+
explanation: "ASP.NET Core project templates are used to create a variety types of new ASP.NET Core projects, including web apps, APIs, and Blazor applications. Project templates provide the essential files, folders, and configurations needed to efficiently set up different types of applications."
34+
- content: "To quickly create various new preconfigured projects with the files and structure needed to get started."
35+
isCorrect: true
36+
explanation: "ASP.NET Core project templates are used to create various new ASP.NET Core projects with the necessary files, folders, and configurations to get a project up and running efficiently."
37+
- content: "They're only useful for creating console apps."
38+
isCorrect: false
39+
explanation: "ASP.NET Core project templates are used to create a variety types of new ASP.NET Core projects, including web apps, APIs, and Blazor applications. Project templates provide the essential files, folders, and configurations needed to efficiently set up different types of applications."
40+
- content: "What .NET CLI command is used to create a new ASP.NET Core web app project from a project template?"
41+
choices:
42+
- content: "`dotnet new <template-name>`"
43+
isCorrect: true
44+
explanation: "The `dotnet new <template-name>` command is used to create a new project from a specified template in .NET CLI."
45+
- content: "`dotnet create-project <template-name>`"
46+
isCorrect: false
47+
explanation: "The .NET CLI command for creating a new project is `dotnet new <template-name>`. The `dotnet create-project` command doesn't exist in the .NET CLI."
48+
- content: "`dotnet init-project <template-name>`"
49+
isCorrect: false
50+
explanation: "The .NET CLI command for creating a new project is `dotnet new <template-name>`. The `dotnet init-project` command doesn't exist in the .NET CLI."
51+
- content: "When should the .NET CLI command `dotnet watch` be used instead of `dotnet run`?"
52+
choices:
53+
- content: "When you want to debug your application with detailed logging."
54+
isCorrect: false
55+
explanation: "`dotnet watch` isn't used for debugging with detailed logging. It's used to watch for file changes and recompile the application during development."
56+
- content: "When you want to run your application with a different framework version."
57+
isCorrect: false
58+
explanation: "`dotnet watch` isn't used for running applications with custom build configurations. It's used to watch for file changes and recompile the application during development."
59+
- content: "When you want to run your application with automatic recompilation on file changes."
60+
isCorrect: true
61+
explanation: "`dotnet watch` is used to run your application and automatically recompile and run it when source files change. This is useful during development to see changes immediately."
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
### YamlMime:ModuleUnit
2+
uid: learn.build-your-first-aspnet-core-web-app.summary
3+
title: Summary
4+
metadata:
5+
title: Summary
6+
description: Summary of building your first ASP.NET Core web app.
7+
ms.date: 9/9/2024
8+
author: wadepickett
9+
ms.author: wpickett
10+
ms.topic: unit
11+
durationInMinutes: 1
12+
content: |
13+
[!include[](includes/7-summary.md)]
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
In this module, you create your first ASP.NET Core web app with .NET and C#.
2+
3+
## Example scenario
4+
5+
You're just getting started with ASP.NET Core. You want to understand how to quickly build your first web app and get familiar with the structure of a basic project. You want to understand how to run and serve a minimal web app on your local machine to view it in a browser.
6+
7+
## What will we be doing?
8+
9+
In this module, you:
10+
11+
- Review ASP.NET Core default project templates available in the .NET SDK.
12+
- Create an ASP.NET Core web app project from a template.
13+
- Examine the structure of the created project.
14+
- Run your web app locally and view it in a browser.
15+
- Review how the web app is served.
16+
- Make code changes during local development.
17+
18+
At the end of this module, there are links to content providing deeper dives for ASP.NET Core web application development.
19+
20+
## Required tools
21+
22+
This module uses [Visual Studio Code with the C# Dev Kit extension](https://code.visualstudio.com/docs/csharp/get-started) to demonstrate creating and running an ASP.NET Core web app.
23+
24+
The following tools are required:
25+
26+
- The latest version of .NET SDK installed from the [.NET website](https://dot.net)
27+
- The latest versions of [Visual Studio Code with C# Dev Kit extension](https://code.visualstudio.com/docs/csharp/get-started) installed.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
Starting a new project, including setting up the initial structure and configurations, can be a daunting task. Fortunately, ASP.NET Core provides various project templates that simplify this process. The project templates offer a standardized and efficient way to kickstart your development. This unit explores the different ASP.NET Core project templates available and how to use them to create new projects.
2+
3+
## What the ASP.NET Core project templates provide
4+
5+
The [.NET SDK](https://dotnet.microsoft.com/download) includes [built-in templates](/dotnet/core/tools/dotnet-new-sdk-templates) for creating ASP.NET Core projects and files. ASP.NET Core project templates are used to create new ASP.NET Core projects with the necessary files, folders, and configurations to get a project up and running efficiently.
6+
7+
With the .NET SDK installed, you can run .NET CLI (Command Line Interface) commands in a terminal or command prompt. To list the built-in templates, run the `dotnet new list` command:
8+
9+
```dotnetcli
10+
dotnet new list
11+
```
12+
13+
Some common ASP.NET Core default project templates include:
14+
15+
`web`
16+
ASP.NET Core Empty: An empty project template for creating an ASP.NET Core application. This template doesn't have any example content in it.
17+
18+
`blazor`
19+
Blazor Web App: A project template for creating a Blazor web app that supports both server-side rendering and client interactivity. This template can be used for web apps with rich dynamic user interfaces (UIs).
20+
21+
`webapi`
22+
ASP.NET Core Web API: A project template for creating a RESTful Web API using ASP.NET Core controllers or minimal APIs, with optional support for OpenAPI and authentication.
23+
24+
`grpc`
25+
ASP.NET Core gRPC Services: A project template for creating a gRPC service using ASP.NET Core.
26+
27+
## Customization options
28+
29+
All .NET project templates offer customization options to tailor the generated project to specific needs and extend it with additional services. For instance, you can add Entity Framework Core for database access, authentication, OpenAPI, caching, logging, and more, enabling the application to effectively meet specific requirements.
30+
31+
To view all customization options for a specific template, use the following `dotnet new` command with the `--help` option, replacing `<template-name>` with the name of the template you want to explore:
32+
33+
```dotnetcli
34+
dotnet new <template-name> --help
35+
```
36+
37+
## Using the .NET CLI `dotnet new` command to create a project
38+
39+
You can create a new project using the `dotnet new <template-name>` command. For example, to create a new empty ASP.NET Core web app project you can run `dotnet new web`.
40+
41+
Once you create a new .NET project, you can build and run the project using the `dotnet build` and `dotnet run` commands.
42+
43+
Alternatively you can create, run, and debug .NET projects with tools like Visual Studio and Visual Studio Code which provide convenient user interfaces.
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
In this exercise, you will:
2+
3+
* Create an ASP.NET Core web app project from a template.
4+
* Examine the structure of the created project.
5+
6+
## Create an ASP.NET Core web app using a template
7+
8+
::: zone pivot="vscode"
9+
10+
In Visual Studio Code, create a new project:
11+
12+
1. Select the **Explorer** view:
13+
14+
:::image type="content" source="../media/vs-code-select-explorer.png" alt-text="Screenshot of selecting the Explorer view.":::
15+
16+
1. Select the **Create .NET Project** button. Alternatively, you can bring up the **Command Palette** using <kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd>, and then type "`.NET`" to find and select the **.NET: New Project** command.
17+
18+
:::image type="content" source="../media/vs-code-select-create-dotnet-project.png" alt-text="Screenshot of selecting Create .NET Project.":::
19+
20+
1. Select the **ASP.NET Core Empty** project template from the list.
21+
1. In the **Project Location** dialog, create a folder named `MyWebApp` to contain the project.
22+
1. In the **Command Palette**, name the project `MyWebApp`, including matching the capitalization. Using this exact project name is important to ensure that the namespaces for code in this instruction match yours.
23+
1. Select **Create project** from the **Command Palette**.
24+
25+
## Examine the structure of the project
26+
27+
The *MyWebApp* project folder contents are displayed in the Visual Studio Code **Explorer**:
28+
29+
:::image type="content" source="../media/vs-code-explorer-project-files.png" alt-text="Screenshot of the project files in the Visual Studio Code Explorer.":::
30+
31+
::: zone-end
32+
33+
::: zone pivot="shell"
34+
35+
From a terminal or the command line, create a new project:
36+
37+
1. Change to the directory (`cd`) that's going to contain the project.
38+
1. Create an ASP.NET Core web app in a directory named *MyWebApp* by running the .NET CLI command `dotnet new`:
39+
40+
```dotnetcli
41+
dotnet new web -o MyWebApp
42+
```
43+
44+
A new ASP.NET Core empty web project is created in a directory named *MyWebApp*.
45+
46+
The following outlines the command syntax:
47+
48+
- `dotnet new`: A .NET CLI command for creating various .NET development artifacts based on templates such as projects, solutions, libraries, configuration, and other specialized files.
49+
- `web`: A project template used to create an ASP.NET Core empty web project, containing no example content. `web` is one of the many [built-in project templates](/dotnet/core/tools/dotnet-new-sdk-templates) available in the [.NET SDK](https://dotnet.microsoft.com/download).
50+
- `-o`: The output option specifies the directory where the new project is created:
51+
- If the directory doesn't exist, the .NET CLI creates it.
52+
- The directory where the project is created serves as the default project name, namespace, and assembly name (the name of the compiled output).
53+
- If the output option `-o <directory>` isn't used, then the current directory is used.
54+
55+
1. Open the *MyWebApp* project folder.
56+
57+
## Examine the structure of the project
58+
59+
::: zone-end
60+
61+
::: zone pivot="vscode"
62+
63+
The *MyWebApp* project folder contents are displayed in the Visual Studio Code **Explorer**:
64+
65+
:::image type="content" source="../media/vs-code-explorer-project-files.png" alt-text="Screenshot of the project files in the Visual Studio Code Explorer.":::
66+
67+
::: zone-end
68+
69+
The following sections contain an overview of the main project folders and files of the empty ASP.NET Core project:
70+
71+
### The *MyWebApp.csproj* project file
72+
73+
The *.csproj* project file is used to:
74+
75+
- Configure how to build the project
76+
- Specify which version of .NET to target
77+
- Manage project dependencies
78+
79+
::: zone pivot="vscode"
80+
### The *.sln* solution file
81+
82+
When an ASP.NET Core project is created or opened in Visual Studio Code (with the C# Dev Kit extension), it creates a *[project name].sln* solution file. The *[project name].sln* solution file contains information for one or more related projects, including build information, settings, and any miscellaneous files that aren’t associated with just one particular project.
83+
84+
::: zone-end
85+
86+
::: zone pivot="shell"
87+
88+
::: zone-end
89+
90+
### The *obj* folder
91+
92+
The `obj` folder contains intermediate files that are used by the build system, including compiled object files generated from the source files. The final build output is placed in a `bin` folder created during the build process.
93+
94+
### The *Properties/launchSettings.json* file
95+
96+
The *Properties/launchSettings.json* file contains configuration data for how the app is launched during development. These settings include the `applicationUrl` property, which specifies the root URL the app uses, such as `https://localhost:{port}`, where `{port}` is a random local port number assigned when the project is created.
97+
98+
The *launchSettings.json* file contains the following configuration:
99+
100+
```json
101+
{
102+
"$schema": "https://json.schemastore.org/launchsettings.json",
103+
"profiles": {
104+
"http": {
105+
"commandName": "Project",
106+
"dotnetRunMessages": true,
107+
"launchBrowser": true,
108+
"applicationUrl": "http://localhost:5218",
109+
"environmentVariables": {
110+
"ASPNETCORE_ENVIRONMENT": "Development"
111+
}
112+
},
113+
"https": {
114+
"commandName": "Project",
115+
"dotnetRunMessages": true,
116+
"launchBrowser": true,
117+
"applicationUrl": "https://localhost:7140;http://localhost:5218",
118+
"environmentVariables": {
119+
"ASPNETCORE_ENVIRONMENT": "Development"
120+
}
121+
}
122+
}
123+
}
124+
```
125+
126+
### The *Program.cs* file
127+
128+
The *Program.cs* file serves as the entry point for an ASP.NET Core app and has several key purposes, which include:
129+
130+
- Host configuration: Configures the host, including setting up the web server.
131+
- Service registration: Adds services to the app’s functionality, such as database contexts, logging, and specialized services for specific frameworks.
132+
- Middleware pipeline configuration: Defines the app’s request handling pipeline as a series of middleware instances.
133+
- Environment configuration: Sets up environment-specific settings for development, staging, and production.
134+
135+
In the new empty ASP.NET Core project you created, the *Program.cs* file contains the following minimal code:
136+
137+
```csharp
138+
var builder = WebApplication.CreateBuilder(args);
139+
var app = builder.Build();
140+
141+
app.MapGet("/", () => "Hello World!");
142+
143+
app.Run();
144+
```
145+
146+
The following lines of code in this file create a `WebApplicationBuilder` with preconfigured defaults, and builds the app:
147+
148+
```csharp
149+
var builder = WebApplication.CreateBuilder(args);
150+
var app = builder.Build();
151+
```
152+
153+
The `app.MapGet()` method directly defines an endpoint that handles HTTP GET requests:
154+
155+
```csharp
156+
app.MapGet("/", () => "Hello World!");
157+
```
158+
159+
`app.MapGet("/")`: Defines a route for the HTTP GET request. The `/` indicates this route responds to the requests made to the root URL of the app. For example, `http://localhost:{port}/`, where `{port}` is a randomly assigned port number assigned in the *Properties/launchSettings.json* file at project creation.
160+
161+
`() => "Hello World!"`: A lambda expression that serves as the request handler. When a GET request is made to the root URL, this lambda expression is executed, and it returns the string "Hello World!"

0 commit comments

Comments
 (0)