Migrating Configuration From ASP.NET MVC 5 to MVC
来源:互联网 发布:c语言求四个数平均值 编辑:程序博客网 时间:2024/05/21 17:13
相关阅读:http://blog.developers.ba/read-config-file-in-asp-net-vnext/
原文:http://aspnetmvc.readthedocs.org/projects/mvc/en/latest/migration/migratingconfig.html
By Steve Smith, Scott Addie
In the previous article, we began migrating an ASP.NET MVC 5 project to MVC 6. In this article, we migrate the configuration feature from ASP.NET MVC 5 to ASP.NET MVC 6.
In this article:
- Setup Configuration
- Migrate Configuration Settings from web.config
- Summary
You can download the finished source from the project created in this article here.
Setup Configuration
ASP.NET 5 and ASP.NET MVC 6 no longer use the Global.asax and web.config files that previous versions of ASP.NET utilized. In earlier versions of ASP.NET, application startup logic was placed in anApplication_StartUp
method within Global.asax. Later, in ASP.NET MVC 5, a Startup.cs file was included in the root of the project; and, it was called using an OwinStartupAttribute
when the application started. ASP.NET 5 (and ASP.NET MVC 6) have adopted this approach completely, placing all startup logic in the Startup.cs file.
The web.config file has also been replaced in ASP.NET 5. 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 5 projects will place configuration values in a JSON-formatted file, such asappsettings.json. ASP.NET 5’s configuration system can also easily access environment variables, which can provide a more secure and robust location for environment-specific values. This is especially true for secrets like connection strings and API keys that should not be checked into source control.
For this article, we are starting with the partially-migrated ASP.NET MVC 6 project from the previous article. To setup configuration using the default MVC 6 settings, add the following constructor and property to the Startup.cs class located in the root of the project:
1 2 3 4 5 6 7 8 910
public Startup(IHostingEnvironment env){ // Set up configuration sources. var builder = new ConfigurationBuilder() .AddJsonFile("appsettings.json") .AddEnvironmentVariables(); Configuration = builder.Build();}public IConfigurationRoot Configuration { get; set; }
Note that at this point the Startup.cs file will not compile, as we still need to add the following using
statement:
using Microsoft.Extensions.Configuration;
Add an appsettings.json file to the root of the project using the appropriate item template:
Migrate Configuration Settings from web.config
Our ASP.NET MVC 5 project included the required database connection string in web.config, in the<connectionStrings>
element. In our MVC 6 project, we are going to store this information in theappsettings.json file. Open appsettings.json, and note that it already includes the following:
1234567
{"Data": {"DefaultConnection": {"ConnectionString": "Server=(localdb)\\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;"}}}
In the highlighted line depicted above, change the name of the database from _CHANGE_ME. We are going to point to a new database, which will be named NewMvc6Project to match our migrated project name.
Summary
ASP.NET 5 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.
- Migrating Configuration From ASP.NET MVC 5 to MVC
- 5 Minute Introduction to ASP.NET MVC
- ASP.NET MVC 5
- ASP.NET MVC 5
- ASP.NET MVC 5
- ASP.NET MVC 5
- ASP.NET MVC 5
- ASP.NET MVC 5
- ASP.NET MVC 5
- ASP.NET MVC 5
- ASP.NET MVC 5
- Programming ASP.NET MVC-Fundamentals of ASP.NET MVC(三)Convention over Configuration
- Authorization in ASP.Net MVC using XML Configuration.
- Intro to ASP.NET MVC 3 (C#)
- ASP.NET MVC-Unable to start iisexpress
- [ASP.Net MVC 5 - 01] ASP.Net MVC and Azure
- Migrating to .NET
- ASP.NET MVC 5 - 控制器
- scoi小凸玩矩阵 匈牙利+二分
- 交叉编译openssl-1.0.0e
- Windows Dev Intro - Error to String
- Cordova--将应用部署在服务器端的实践
- Android Studio 中常用的插件
- Migrating Configuration From ASP.NET MVC 5 to MVC
- 读《Unity3D游戏开发》笔记工具类
- 平台基础环境的内置函数介绍
- 网络实时监测广播类
- 正则表达式的使用
- zTree树形结构框架连接数据库实现增删改
- C++的深拷贝与浅拷贝
- CCF 201509-3 模板生成系统
- Anrdoid 面试技巧