Skip to content

Commit e5862c2

Browse files
authored
Merge pull request #183976 from AlexandraKemperMS/Net6ASPNETConfig
Adding .NET 6 support for ASP.NET Quicktstart
2 parents 8f620ba + 512dec0 commit e5862c2

File tree

4 files changed

+126
-49
lines changed

4 files changed

+126
-49
lines changed

articles/azure-app-configuration/quickstart-aspnet-core-app.md

Lines changed: 100 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.service: azure-app-configuration
77
ms.devlang: csharp
88
ms.custom: devx-track-csharp, contperf-fy21q1, mode-other
99
ms.topic: quickstart
10-
ms.date: 09/25/2020
10+
ms.date: 1/3/2022
1111
ms.author: alkemper
1212
#Customer intent: As an ASP.NET Core developer, I want to learn how to manage all my app settings in one place.
1313
---
@@ -71,61 +71,115 @@ dotnet new mvc --no-https --output TestAppConfig
7171
7272
Access this secret using the .NET Core Configuration API. A colon (`:`) works in the configuration name with the Configuration API on all supported platforms. For more information, see [Configuration keys and values](/aspnet/core/fundamentals/configuration#configuration-keys-and-values).
7373
74-
1. In *Program.cs*, add a reference to the .NET Core Configuration API namespace:
74+
1. Select the correct syntax based on your environment.
7575
76-
```csharp
77-
using Microsoft.Extensions.Configuration;
78-
```
79-
80-
1. Update the `CreateWebHostBuilder` method to use App Configuration by calling the `AddAzureAppConfiguration` method.
81-
82-
> [!IMPORTANT]
83-
> `CreateHostBuilder` replaces `CreateWebHostBuilder` in .NET Core 3.x. Select the correct syntax based on your environment.
84-
85-
#### [.NET 5.x](#tab/core5x)
76+
#### [.NET 6.x](#tab/core6x)
77+
In *Program.cs*, replace its content with the following code:
8678
8779
```csharp
88-
public static IHostBuilder CreateHostBuilder(string[] args) =>
89-
Host.CreateDefaultBuilder(args)
90-
.ConfigureWebHostDefaults(webBuilder =>
91-
webBuilder.ConfigureAppConfiguration(config =>
92-
{
93-
var settings = config.Build();
94-
var connection = settings.GetConnectionString("AppConfig");
95-
config.AddAzureAppConfiguration(connection);
96-
}).UseStartup<Startup>());
80+
var builder = WebApplication.CreateBuilder(args);
81+
//Retrieve the Connection String from the secrets manager
82+
var connectionString = builder.Configuration["AppConfig"];
83+
84+
builder.Host.ConfigureAppConfiguration(builder =>
85+
{
86+
//Connect to your App Config Store using the connection string
87+
builder.AddAzureAppConfiguration(connectionString);
88+
})
89+
.ConfigureServices(services =>
90+
{
91+
services.AddControllersWithViews();
92+
});
93+
94+
var app = builder.Build();
95+
96+
// Configure the HTTP request pipeline.
97+
if (!app.Environment.IsDevelopment())
98+
{
99+
app.UseExceptionHandler("/Home/Error");
100+
}
101+
app.UseStaticFiles();
102+
103+
app.UseRouting();
104+
105+
app.UseAuthorization();
106+
107+
app.MapControllerRoute(
108+
name: "default",
109+
pattern: "{controller=Home}/{action=Index}/{id?}");
110+
111+
app.Run();
97112
```
113+
114+
#### [.NET 5.x](#tab/core5x)
115+
116+
1. In *Program.cs*, add a reference to the .NET Core Configuration API namespace:
117+
118+
```csharp
119+
using Microsoft.Extensions.Configuration;
120+
```
121+
122+
1. Update the `CreateHostBuilder` method to use App Configuration by calling the `AddAzureAppConfiguration` method.
123+
124+
```csharp
125+
public static IHostBuilder CreateHostBuilder(string[] args) =>
126+
Host.CreateDefaultBuilder(args)
127+
.ConfigureWebHostDefaults(webBuilder =>
128+
webBuilder.ConfigureAppConfiguration(config =>
129+
{
130+
var settings = config.Build();
131+
var connection = settings.GetConnectionString("AppConfig");
132+
config.AddAzureAppConfiguration(connection);
133+
}).UseStartup<Startup>());
134+
```
98135
#### [.NET Core 3.x](#tab/core3x)
99136
100-
```csharp
101-
public static IHostBuilder CreateHostBuilder(string[] args) =>
102-
Host.CreateDefaultBuilder(args)
103-
.ConfigureWebHostDefaults(webBuilder =>
104-
webBuilder.ConfigureAppConfiguration(config =>
137+
> [!IMPORTANT]
138+
> `CreateHostBuilder` in .NET 3.x replaces `CreateWebHostBuilder` in .NET Core 2.x.
139+
140+
1. In *Program.cs*, add a reference to the .NET Core Configuration API namespace:
141+
142+
```csharp
143+
using Microsoft.Extensions.Configuration;
144+
```
145+
1. Update the `CreateHostBuilder` method to use App Configuration by calling the `AddAzureAppConfiguration` method.
146+
147+
```csharp
148+
public static IHostBuilder CreateHostBuilder(string[] args) =>
149+
Host.CreateDefaultBuilder(args)
150+
.ConfigureWebHostDefaults(webBuilder =>
151+
webBuilder.ConfigureAppConfiguration(config =>
152+
{
153+
var settings = config.Build();
154+
var connection = settings.GetConnectionString("AppConfig");
155+
config.AddAzureAppConfiguration(connection);
156+
}).UseStartup<Startup>());
157+
```
158+
159+
#### [.NET Core 2.x](#tab/core2x)
160+
161+
1. In *Program.cs*, add a reference to the .NET Core Configuration API namespace:
162+
163+
```csharp
164+
using Microsoft.Extensions.Configuration;
165+
```
166+
167+
1. Update the `CreateWebHostBuilder` method to use App Configuration by calling the `AddAzureAppConfiguration` method.
168+
169+
```csharp
170+
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
171+
WebHost.CreateDefaultBuilder(args)
172+
.ConfigureAppConfiguration(config =>
105173
{
106174
var settings = config.Build();
107175
var connection = settings.GetConnectionString("AppConfig");
108176
config.AddAzureAppConfiguration(connection);
109-
}).UseStartup<Startup>());
110-
```
111-
112-
#### [.NET Core 2.x](#tab/core2x)
113-
114-
```csharp
115-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
116-
WebHost.CreateDefaultBuilder(args)
117-
.ConfigureAppConfiguration(config =>
118-
{
119-
var settings = config.Build();
120-
var connection = settings.GetConnectionString("AppConfig");
121-
config.AddAzureAppConfiguration(connection);
122-
})
123-
.UseStartup<Startup>();
124-
```
125-
126-
---
177+
})
178+
.UseStartup<Startup>();
179+
```
180+
---
127181
128-
With the preceding change, the [configuration provider for App Configuration](/dotnet/api/Microsoft.Extensions.Configuration.AzureAppConfiguration) has been registered with the .NET Core Configuration API.
182+
This code will connect to your App Configuration store using a connection string and load all keys that have the *TestApp* prefix from a previous step. For more information on the configuration provider APIs, reference the [configuration provider for App Configuration docs](/dotnet/api/Microsoft.Extensions.Configuration.AzureAppConfiguration).
129183
130184
## Read from the App Configuration store
131185
@@ -171,7 +225,7 @@ In the preceding code, the App Configuration store's keys are used as follows:
171225
dotnet run
172226
```
173227
174-
1. If you're working on your local machine, use a browser to navigate to `http://localhost:5000`. This address is the default URL for the locally hosted web app. If you're working in the Azure Cloud Shell, select the **Web Preview** button followed by **Configure**.
228+
1. If you're working on your local machine, use a browser to navigate to `http://localhost:5000` or as specified in the command output. This address is the default URL for the locally hosted web app. If you're working in the Azure Cloud Shell, select the **Web Preview** button followed by **Configure**.
175229
176230
![Locate the Web Preview button](./media/quickstarts/cloud-shell-web-preview.png)
177231

includes/azure-app-configuration-add-secret-manager.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@
22

33
A tool called Secret Manager stores sensitive data for development work outside of your project tree. This approach helps prevent the accidental sharing of app secrets within source code. Complete the following steps to enable the use of Secret Manager in the ASP.NET Core project:
44

5+
#### [.NET 6.x](#tab/core6x)
6+
7+
Navigate to the project's root directory, and run the following command to enable secrets storage in the project:
8+
9+
```dotnetcli
10+
dotnet user-secrets init
11+
```
12+
13+
A `UserSecretsId` element containing a GUID is added to the *.csproj* file:
14+
15+
```xml
16+
<Project Sdk="Microsoft.NET.Sdk.Web">
17+
18+
<PropertyGroup>
19+
<TargetFramework>net6.0</TargetFramework>
20+
<Nullable>enable</Nullable>
21+
<ImplicitUsings>enable</ImplicitUsings>
22+
<UserSecretsId>8296b5b7-6db3-4ae9-a590-899ac642c0d7</UserSecretsId>
23+
</PropertyGroup>
24+
25+
</Project>
26+
```
527
#### [.NET 5.x](#tab/core5x)
628

729
Navigate to the project's root directory, and run the following command to enable secrets storage in the project:

includes/azure-app-configuration-create.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ ms.date: 1/31/2020
77
---
88

99
1. To create a new App Configuration store, sign in to the [Azure portal](https://portal.azure.com). In the upper-left corner of the home page, select **Create a resource**. In the **Search the Marketplace** box, enter *App Configuration* and select <kbd>Enter</kbd>.
10-
11-
![Search for App Configuration](media/azure-app-configuration-create/azure-portal-search.png)
10+
11+
:::image type="content" source="media/azure-app-configuration-create/azure-portal-search.png" alt-text="Search for App Configuration":::
12+
1213

1314
1. Select **App Configuration** from the search results, and then select **Create**.
1415

15-
![Select Create](media/azure-app-configuration-create/azure-portal-app-configuration-create.png)
16+
:::image type="content" source="media/azure-app-configuration-create/azure-portal-app-configuration-create.png" alt-text="Select Create":::
1617

1718
1. On the **Create App Configuration** pane, enter the following settings:
1819

1000 KB
Loading

0 commit comments

Comments
 (0)