AspNetCore.Docs/aspnetcore/migration/configuration.md

48 lines
3.5 KiB
Markdown
Raw Normal View History

2016-10-29 01:35:15 +08:00
---
title: Migrate configuration to ASP.NET Core
2016-11-12 00:28:14 +08:00
author: ardalis
description: Learn how to migrate configuration from an ASP.NET MVC project to an ASP.NET Core MVC project.
2018-01-29 23:21:31 +08:00
ms.author: riande
2016-10-29 01:35:15 +08:00
ms.date: 10/14/2016
uid: migration/configuration
---
# Migrate configuration to ASP.NET Core
2016-10-29 01:35:15 +08:00
By [Steve Smith](https://ardalis.com/) and [Scott Addie](https://scottaddie.com)
2016-10-29 01:35:15 +08:00
2019-10-28 20:48:10 +08:00
In the previous article, we began to [migrate an ASP.NET MVC project to ASP.NET Core MVC](xref:migration/mvc). In this article, we migrate configuration.
2016-10-29 01:35:15 +08:00
2021-03-10 12:40:27 +08:00
[View or download sample code](https://github.com/dotnet/AspNetCore.Docs/tree/main/aspnetcore/migration/configuration/samples) ([how to download](xref:index#how-to-download-a-sample))
2016-10-29 01:35:15 +08:00
## Setup configuration
2016-10-29 01:35:15 +08:00
2022-03-22 06:24:36 +08:00
ASP.NET Core no longer uses the *Global.asax* and *web.config* files that previous versions of ASP.NET utilized. In the earlier versions of ASP.NET, application startup logic was placed in an `Application_StartUp` method within *Global.asax*. Later, in ASP.NET MVC, a `Startup.cs` file was included in the root of the project; and, it was called when the application started. ASP.NET Core has adopted this approach completely by placing all startup logic in the `Startup.cs` file.
2016-10-29 01:35:15 +08:00
2022-03-22 06:24:36 +08:00
The *web.config* file has also been replaced in ASP.NET Core. Configuration itself can now be configured, as part of the application startup procedure described in `Startup.cs`. Configuration can still utilize XML files, but typically ASP.NET Core projects will place configuration values in a JSON-formatted file, such as `appsettings.json`. ASP.NET Core's configuration system can also easily access environment variables, which can provide a [more secure and robust location](xref:security/app-secrets) for environment-specific values. This is especially true for secrets like connection strings and API keys that shouldn't be checked into source control. See [Configuration](xref:fundamentals/configuration/index) to learn more about configuration in ASP.NET Core.
2016-10-29 01:35:15 +08:00
2022-03-22 06:24:36 +08:00
For this article, we are starting with the partially migrated ASP.NET Core project from [the previous article](xref:migration/mvc). To setup configuration, add the following constructor and property to the `Startup.cs` file located in the root of the project:
2016-10-29 01:35:15 +08:00
[!code-csharp[](configuration/samples/WebApp1/src/WebApp1/Startup.cs?range=11-16)]
2016-10-29 01:35:15 +08:00
2022-03-22 06:24:36 +08:00
Note that at this point, the `Startup.cs` file won't compile, as we still need to add the following `using` statement:
2016-10-29 01:35:15 +08:00
2016-11-18 13:03:07 +08:00
```csharp
2016-10-29 01:35:15 +08:00
using Microsoft.Extensions.Configuration;
2016-11-18 13:03:07 +08:00
```
2016-10-29 01:35:15 +08:00
2022-03-22 06:24:36 +08:00
Add an `appsettings.json` file to the root of the project using the appropriate item template:
2016-10-29 01:35:15 +08:00
![Add AppSettings JSON](configuration/_static/add-appsettings-json.png)
2016-10-29 01:35:15 +08:00
## Migrate configuration settings from web.config
2016-10-29 01:35:15 +08:00
2022-03-22 06:24:36 +08:00
Our ASP.NET MVC project included the required database connection string in *web.config*, in the `<connectionStrings>` element. In our ASP.NET Core project, we are going to store this information in the `appsettings.json` file. Open `appsettings.json`, and note that it already includes the following:
2016-10-29 01:35:15 +08:00
[!code-json[](../migration/configuration/samples/WebApp1/src/WebApp1/appsettings.json?highlight=4)]
2016-10-29 01:35:15 +08:00
In the highlighted line depicted above, change the name of the database from **_CHANGE_ME** to the name of your database.
## Summary
ASP.NET Core places all startup logic for the application in a single file, in which the necessary services and dependencies can be defined and configured. It replaces the *web.config* file with a flexible configuration feature that can leverage a variety of file formats, such as JSON, as well as environment variables.