Skip to content

Commit 6a25fe6

Browse files
committed
moniker prep
1 parent eea43c2 commit 6a25fe6

File tree

2 files changed

+237
-1
lines changed

2 files changed

+237
-1
lines changed

aspnetcore/fundamentals/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ uid: fundamentals/index
1212

1313
[!INCLUDE[](~/includes/not-latest-version.md)]
1414

15-
:::moniker range=">= aspnetcore-8.0"
15+
:::moniker range=">= aspnetcore-9.0"
1616

1717
This article provides an overview of the fundamentals for building ASP.NET Core apps, including dependency injection (DI), configuration, middleware, and more.
1818

@@ -232,3 +232,4 @@ For more information, see <xref:fundamentals/static-files>.
232232
:::moniker-end
233233

234234
[!INCLUDE[](~/fundamentals/index/includes/index3-7.md)]
235+
[!INCLUDE[](~/fundamentals/index/includes/index8.md)]
Lines changed: 235 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,235 @@
1+
:::moniker range="= aspnetcore-8.0"
2+
3+
---
4+
title: ASP.NET Core fundamentals overview
5+
author: tdykstra
6+
description: Learn the fundamental concepts for building ASP.NET Core apps, including dependency injection (DI), configuration, middleware, and more.
7+
monikerRange: '>= aspnetcore-3.1'
8+
ms.author: tdykstra
9+
ms.custom: mvc
10+
ms.date: 08/01/2024
11+
uid: fundamentals/index
12+
---
13+
# ASP.NET Core fundamentals overview
14+
15+
[!INCLUDE[](~/includes/not-latest-version.md)]
16+
17+
:::moniker range=">= aspnetcore-8.0"
18+
19+
This article provides an overview of the fundamentals for building ASP.NET Core apps, including dependency injection (DI), configuration, middleware, and more.
20+
21+
For Blazor fundamentals guidance, which adds to or supersedes the guidance in this node, see <xref:blazor/fundamentals/index>.
22+
23+
## Program.cs
24+
25+
ASP.NET Core apps created with the web templates contain the application startup code in the `Program.cs` file. The `Program.cs` file is where:
26+
27+
* Services required by the app are configured.
28+
* The app's request handling pipeline is defined as a series of [middleware components](xref:fundamentals/middleware/index).
29+
30+
The following app startup code supports:
31+
32+
* [Razor Pages](xref:tutorials/razor-pages/razor-pages-start)
33+
* [MVC controllers with views](xref:tutorials/first-mvc-app/start-mvc)
34+
* [Web API with controllers](xref:tutorials/first-web-api)
35+
* [Minimal web APIs](xref:tutorials/min-web-api)
36+
37+
[!code-csharp[](~/fundamentals/startup/6.0_samples/WebAll/Program.cs?name=snippet)]
38+
39+
## Dependency injection (services)
40+
41+
ASP.NET Core includes [dependency injection (DI)](xref:fundamentals/dependency-injection) that makes configured services available throughout an app. Services are added to the DI container with [WebApplicationBuilder.Services](xref:Microsoft.AspNetCore.Builder.WebApplicationBuilder.Services), `builder.Services` in the preceding code. When the <xref:Microsoft.AspNetCore.Builder.WebApplicationBuilder> is instantiated, many [framework-provided services](xref:fundamentals/dependency-injection#framework-provided-services) are added. `builder` is a `WebApplicationBuilder` in the following code:
42+
43+
[!code-csharp[](~/fundamentals/startup/6.0_samples/WebAll/Program.cs?name=snippet2&highlight=1)]
44+
45+
In the preceding highlighted code, `builder` has configuration, logging, and [many other services](xref:fundamentals/dependency-injection#framework-provided-services) added to the DI container.
46+
47+
The following code adds Razor Pages, MVC controllers with views, and a custom <xref:Microsoft.EntityFrameworkCore.DbContext> to the DI container:
48+
49+
[!code-csharp[](~/fundamentals/index/samples/6.0/RazorPagesMovie/Program.cs?name=snippet2&highlight=6-10)]
50+
51+
Services are typically resolved from DI using constructor injection. The DI framework provides an instance of this service at runtime.
52+
53+
The following code uses constructor injection to resolve the database context and logger from DI:
54+
55+
[!code-csharp[](~/fundamentals/index/samples/6.0/RazorPagesMovie/Pages/Movies/Index.cshtml.cs?name=snippet&highlight=3-10, 16-17)]
56+
57+
## Middleware
58+
59+
The request handling pipeline is composed as a series of middleware components. Each component performs operations on an [`HttpContext`](xref:fundamentals/httpcontext) and either invokes the next middleware in the pipeline or terminates the request.
60+
61+
By convention, a middleware component is added to the pipeline by invoking a `Use{Feature}` extension method. Middleware added to the app is highlighted in the following code:
62+
63+
[!code-csharp[](~/fundamentals/startup/6.0_samples/WebAll/Program.cs?name=snippet&highlight=12-19)]
64+
65+
For more information, see <xref:fundamentals/middleware/index>.
66+
67+
## Host
68+
69+
On startup, an ASP.NET Core app builds a *host*. The host encapsulates all of the app's resources, such as:
70+
71+
* An HTTP server implementation
72+
* Middleware components
73+
* Logging
74+
* Dependency injection (DI) services
75+
* Configuration
76+
77+
There are three different hosts capable of running an ASP.NET Core app:
78+
79+
* [ASP.NET Core WebApplication](xref:fundamentals/minimal-apis/webapplication), also known as the [Minimal Host](xref:migration/50-to-60#new-hosting-model)
80+
* [.NET Generic Host](xref:fundamentals/host/generic-host) combined with ASP.NET Core's <xref:Microsoft.Extensions.Hosting.GenericHostBuilderExtensions.ConfigureWebHostDefaults%2A>
81+
* [ASP.NET Core WebHost](xref:fundamentals/host/web-host)
82+
83+
The ASP.NET Core <xref:Microsoft.AspNetCore.Builder.WebApplication> and <xref:Microsoft.AspNetCore.Builder.WebApplicationBuilder> types are recommended and used in all the ASP.NET Core templates. `WebApplication` behaves similarly to the .NET Generic Host and exposes many of the same interfaces but requires less callbacks to configure. The ASP.NET Core <xref:Microsoft.AspNetCore.WebHost> is available only for backward compatibility.
84+
85+
The following example instantiates a `WebApplication`:
86+
87+
[!code-csharp[](~/fundamentals/startup/6.0_samples/WebAll/Program.cs?name=snippet2&highlight=7)]
88+
89+
The [WebApplicationBuilder.Build](xref:Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build%2A) method configures a host with a set of default options, such as:
90+
91+
* Use [Kestrel](#servers) as the web server and enable IIS integration.
92+
* Load [configuration](xref:fundamentals/configuration/index) from `appsettings.json`, environment variables, command line arguments, and other configuration sources.
93+
* Send logging output to the console and debug providers.
94+
95+
### Non-web scenarios
96+
97+
The Generic Host allows other types of apps to use cross-cutting framework extensions, such as logging, dependency injection (DI), configuration, and app lifetime management. For more information, see <xref:fundamentals/host/generic-host> and <xref:fundamentals/host/hosted-services>.
98+
99+
## Servers
100+
101+
An ASP.NET Core app uses an HTTP server implementation to listen for HTTP requests. The server surfaces requests to the app as a set of [request features](xref:fundamentals/request-features) composed into an `HttpContext`.
102+
103+
# [Windows](#tab/windows)
104+
105+
ASP.NET Core provides the following server implementations:
106+
107+
* *Kestrel* is a cross-platform web server. Kestrel is often run in a reverse proxy configuration using [IIS](https://www.iis.net/). In ASP.NET Core 2.0 or later, Kestrel can be run as a public-facing edge server exposed directly to the Internet.
108+
* *IIS HTTP Server* is a server for Windows that uses IIS. With this server, the ASP.NET Core app and IIS run in the same process.
109+
* *HTTP.sys* is a server for Windows that isn't used with IIS.
110+
111+
# [macOS](#tab/macos)
112+
113+
ASP.NET Core provides the *Kestrel* cross-platform server implementation. In ASP.NET Core 2.0 or later, Kestrel can run as a public-facing edge server exposed directly to the Internet. Kestrel is often run in a reverse proxy configuration with [Nginx](https://nginx.org) or [Apache](https://httpd.apache.org/).
114+
115+
# [Linux](#tab/linux)
116+
117+
ASP.NET Core provides the *Kestrel* cross-platform server implementation. In ASP.NET Core 2.0 or later, Kestrel can run as a public-facing edge server exposed directly to the Internet. Kestrel is often run in a reverse proxy configuration with [Nginx](https://nginx.org) or [Apache](https://httpd.apache.org/).
118+
119+
---
120+
121+
For more information, see <xref:fundamentals/servers/index>.
122+
123+
## Configuration
124+
125+
ASP.NET Core provides a [configuration](xref:fundamentals/configuration/index) framework that gets settings as name-value pairs from an ordered set of configuration providers. Built-in configuration providers are available for a variety of sources, such as `.json` files, `.xml` files, environment variables, and command-line arguments. Write custom configuration providers to support other sources.
126+
127+
By [default](xref:fundamentals/configuration/index#default), ASP.NET Core apps are configured to read from `appsettings.json`, environment variables, the command line, and more. When the app's configuration is loaded, values from environment variables override values from `appsettings.json`.
128+
129+
For managing confidential configuration data such as passwords, .NET Core provides the [Secret Manager](xref:security/app-secrets#secret-manager). For production secrets, we recommend [Azure Key Vault](xref:security/key-vault-configuration).
130+
131+
For more information, see <xref:fundamentals/configuration/index>.
132+
133+
## Environments
134+
135+
Execution environments, such as `Development`, `Staging`, and `Production`, are available in ASP.NET Core. Specify the environment an app is running in by setting the `ASPNETCORE_ENVIRONMENT` environment variable. ASP.NET Core reads that environment variable at app startup and stores the value in an `IWebHostEnvironment` implementation. This implementation is available anywhere in an app via dependency injection (DI).
136+
137+
The following example configures the exception handler and [HTTP Strict Transport Security Protocol (HSTS)](xref:security/enforcing-ssl#http-strict-transport-security-protocol-hsts) middleware when ***not*** running in the `Development` environment:
138+
139+
[!code-csharp[](~/fundamentals/startup/6.0_samples/WebAll/Program.cs?name=snippet&highlight=10-14)]
140+
141+
For more information, see <xref:fundamentals/environments>.
142+
143+
## Logging
144+
145+
ASP.NET Core supports a logging API that works with a variety of built-in and third-party logging providers. Available providers include:
146+
147+
* Console
148+
* Debug
149+
* Event Tracing on Windows
150+
* Windows Event Log
151+
* TraceSource
152+
* Azure App Service
153+
* Azure Application Insights
154+
155+
To create logs, resolve an <xref:Microsoft.Extensions.Logging.ILogger%601> service from dependency injection (DI) and call logging methods such as <xref:Microsoft.Extensions.Logging.LoggerExtensions.LogInformation%2A>. For example:
156+
157+
[!code-csharp[](~/fundamentals/index/samples/6.0/RazorPagesMovie/Pages/Movies/Index.cshtml.cs?name=snippet&highlight=3-10, 16-17)]
158+
159+
For more information, see <xref:fundamentals/logging/index>.
160+
161+
## Routing
162+
163+
A *route* is a URL pattern that is mapped to a handler. The handler is typically a Razor page, an action method in an MVC controller, or a middleware. ASP.NET Core routing gives you control over the URLs used by your app.
164+
165+
The following code, generated by the ASP.NET Core web application template, calls <xref:Microsoft.AspNetCore.Builder.EndpointRoutingApplicationBuilderExtensions.UseRouting%2A>:
166+
167+
[!code-csharp[](~/fundamentals/startup/6.0_samples/WebAll/Program.cs?name=snippet4&highlight=17)]
168+
169+
For more information, see <xref:fundamentals/routing>.
170+
171+
## Error handling
172+
173+
ASP.NET Core has built-in features for handling errors, such as:
174+
175+
* A developer exception page
176+
* Custom error pages
177+
* Static status code pages
178+
* Startup exception handling
179+
180+
For more information, see <xref:fundamentals/error-handling>.
181+
182+
## Make HTTP requests
183+
184+
An implementation of `IHttpClientFactory` is available for creating `HttpClient` instances. The factory:
185+
186+
* Provides a central location for naming and configuring logical `HttpClient` instances. For example, register and configure a *github* client for accessing GitHub. Register and configure a default client for other purposes.
187+
* Supports registration and chaining of multiple delegating handlers to build an outgoing request middleware pipeline. This pattern is similar to ASP.NET Core's inbound middleware pipeline. The pattern provides a mechanism to manage cross-cutting concerns for HTTP requests, including caching, error handling, serialization, and logging.
188+
* Integrates with *Polly*, a popular third-party library for transient fault handling.
189+
* Manages the pooling and lifetime of underlying `HttpClientHandler` instances to avoid common DNS problems that occur when managing `HttpClient` lifetimes manually.
190+
* Adds a configurable logging experience via <xref:Microsoft.Extensions.Logging.ILogger> for all requests sent through clients created by the factory.
191+
192+
For more information, see <xref:fundamentals/http-requests>.
193+
194+
## Content root
195+
196+
The content root is the base path for:
197+
198+
* The executable hosting the app (*.exe*).
199+
* Compiled assemblies that make up the app (*.dll*).
200+
* Content files used by the app, such as:
201+
* Razor files (`.cshtml`, `.razor`)
202+
* Configuration files (`.json`, `.xml`)
203+
* Data files (`.db`)
204+
* The [Web root](#web-root), typically the *wwwroot* folder.
205+
206+
During development, the content root defaults to the project's root directory. This directory is also the base path for both the app's content files and the [Web root](#web-root). Specify a different content root by setting its path when [building the host](#host). For more information, see [Content root](xref:fundamentals/host/generic-host#contentroot).
207+
208+
## Web root
209+
210+
The web root is the base path for public, static resource files, such as:
211+
212+
* Stylesheets (`.css`)
213+
* JavaScript (`.js`)
214+
* Images (`.png`, `.jpg`)
215+
216+
By default, static files are served only from the web root directory and its sub-directories. The web root path defaults to *{content root}/wwwroot*. Specify a different web root by setting its path when [building the host](#host). For more information, see [Web root](xref:fundamentals/host/generic-host#webroot).
217+
218+
Prevent publishing files in *wwwroot* with the [\<Content> project item](/visualstudio/msbuild/common-msbuild-project-items#content) in the project file. The following example prevents publishing content in *wwwroot/local* and its sub-directories:
219+
220+
```xml
221+
<ItemGroup>
222+
<Content Update="wwwroot\local\**\*.*" CopyToPublishDirectory="Never" />
223+
</ItemGroup>
224+
```
225+
226+
In Razor `.cshtml` files, `~/` points to the web root. A path beginning with `~/` is referred to as a *virtual path*.
227+
228+
For more information, see <xref:fundamentals/static-files>.
229+
230+
## Additional resources
231+
232+
* [WebApplicationBuilder source code](https://github.com/dotnet/aspnetcore/blob/v6.0.1/src/DefaultBuilder/src/WebApplicationBuilder.cs)
233+
234+
:::moniker-end
235+

0 commit comments

Comments
 (0)