Migrating Configuration From ASP.NET MVC 5 to MVC

本文详细介绍了如何将ASP.NET MVC5项目的配置迁移到MVC6,包括配置文件的替换和使用JSON文件存储配置值,以及如何在Startup.cs文件中设置配置来源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

相关阅读:http://blog.developers.ba/read-config-file-in-asp-net-vnext/

原文:http://aspnetmvc.readthedocs.org/projects/mvc/en/latest/migration/migratingconfig.html

By Steve SmithScott 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:

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
 9
10
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 usingstatement:

using Microsoft.Extensions.Configuration;

Add an appsettings.json file to the root of the project using the appropriate item template:

../_images/add-appsettings-json.png

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:

1
2
3
4
5
6
7
{
	"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.



<think>嗯,用户问的是如何将ASP网页改为ASP.NET。首先,我需要确认用户所说的ASP指的是经典的ASP,也就是ASP 3.0,而不是ASP.NET。因为这两个技术虽然名字相似,但差别挺大的。用户可能有一个旧的ASP网站,想要迁移到更新的ASP.NET平台,可能是为了更好的性能、安全性或者利用.NET框架的功能。 接下来,我应该考虑用户的需求是什么。他们可能遇到了维护困难,或者想要使用更现代的Web开发技术。ASP.NET提供了更多的功能,比如MVC模式、Web Forms,以及后来的ASP.NET Core,这些都是优势。但迁移过程可能会有很多挑战,比如代码结构的改变,语言从VBScript/JScript转到C#或VB.NET,还有状态管理、数据库连接方式的不同。 然后,我需要分步骤来介绍迁移过程。首先,用户需要了解两者的区别,然后评估现有代码,确定哪些部分可以重用,哪些需要重写。可能还需要考虑逐步迁移还是一次性迁移。如果是大型项目,逐步迁移可能更可行,比如将部分页面转为ASP.NET,同时保留旧有的ASP部分,但需要注意会话状态和应用程序变量的共享问题。 另外,用户可能对工具和资源有需求,比如Visual Studio的迁移工具,或者如何配置IIS来支持两者并存。还需要提醒用户注意常见的陷阱,比如表单验证的不同、COM组件的替换,以及安全性方面的提升,比如ASP.NET内置的防跨站脚本攻击功能。 可能用户没有提到的深层需求是,他们可能希望提高网站的性能和可维护性,或者整合到现有的.NET生态系统中。这时候,ASP.NET Core可能是一个更好的长期选择,但迁移路径可能需要更多的调整。需要根据项目的具体情况来决定是否直接迁移到最新的ASP.NET Core还是先转到较旧的ASP.NET Web Forms或MVC。 还需要考虑数据库访问层的变化,经典ASP通常使用ADO,而ASP.NET可以使用ADO.NET或Entity Framework,这可能涉及代码的重构。此外,用户界面部分,如果原来是混合HTML和服务器脚本,可能需要转换为服务器控件或Razor语法,这取决于选择的ASP.NET框架。 最后,应该提供一些资源或工具,比如微软的官方文档,社区论坛,或者推荐使用Visual Studio的迁移向导。同时,提醒用户在迁移后进行全面的测试,包括功能测试、性能测试和安全测试,确保没有遗漏的问题。</think>将经典的 **ASP(Active Server Pages)** 网页迁移到 **ASP.NET** 是一个逐步升级和重构的过程,涉及技术架构、代码逻辑和开发模式的调整。以下是迁移的主要步骤和注意事项: --- ### **1. 理解两者的核心区别** - **经典 ASP** - 使用 VBScript/JScript 语言,代码与 HTML 混合(类似 PHP)。 - 依赖 COM 组件,功能扩展性有限。 - 无状态管理、安全性较弱,性能较低。 - **ASP.NET** - 支持 C#、VB.NET 等现代语言,代码与界面可分离(如 Web Forms 或 MVC)。 - 基于 .NET Framework/Core,提供丰富的类库和工具(如 ADO.NET、Entity Framework)。 - 内置状态管理(ViewState、Session)、安全机制(身份验证、防 XSS)和性能优化。 --- ### **2. 迁移步骤** #### **(1) 评估现有代码** - 分析现有 ASP 页面的功能、数据库交互和依赖的 COM 组件。 - 确定哪些代码可直接复用(如纯 HTML/CSS/JS),哪些需要重写(如数据库操作、业务逻辑)。 #### **(2) 选择 ASP.NET 技术栈** - **ASP.NET Web Forms** 适合快速迁移,提供类似经典 ASP 的“控件+事件驱动”模型,但需注意 ViewState 的性能影响。 - **ASP.NET MVC** 更清晰的分层架构(Model-View-Controller),适合复杂项目。 - **ASP.NET Core** 跨平台、高性能,但需全面重构,适合长期维护的新项目。 #### **(3) 逐步重构代码** - **分离逻辑与界面** 将业务逻辑移到 `.cs`/`.vb` 代码文件中(如 Web Forms 的后台代码或 MVC 的控制器)。 - **替换数据库操作** 将 `ADO` 转换为 `ADO.NET` 或 ORM 框架(如 Entity Framework),使用参数化查询防 SQL 注入。 - **重写 COM 组件** 用 .NET 类库替代原有的 COM 功能(如文件操作、邮件发送)。 #### **(4) 状态管理调整** - 经典 ASP 的 `Session` 和 `Application` 对象在 ASP.NET 中仍可用,但需注意类型安全(需强制转换)。 - 使用 ASP.NET 的 `ViewState` 或 `Cache` 优化页面状态管理。 #### **(5) 配置部署环境** - **IIS 配置** ASP.NET 需要安装对应的 .NET Framework 版本(如 4.8)或 ASP.NET Core Runtime。 - **兼容性处理** 若需新旧系统共存,可通过子目录或虚拟目录隔离 ASPASP.NET 页面。 --- ### **3. 常见挑战与解决方案** - **会话共享问题** ASPASP.NET 的 Session 默认不互通,可通过数据库存储会话数据或使用中间件同步。 - **URL 兼容性** 保留原有 URL 结构,通过路由配置(如 ASP.NET MVC 的 `RouteConfig`)实现重定向。 - **安全性提升** 启用 ASP.NET 的防跨站脚本(`ValidateRequest`)、表单身份验证(`FormsAuthentication`)等机制。 --- ### **4. 工具与资源** - **Visual Studio** 内置迁移工具和调试支持,可自动识别部分兼容性问题。 - **Microsoft Interop Forms Toolkit** 辅助迁移 COM 组件的工具(适用于简单场景)。 - **官方文档** - [ASP.NET 迁移指南](https://learn.microsoft.com/aspnet/migration) - [从经典 ASPASP.NET Core 的升级策略](https://devblogs.microsoft.com/dotnet/migrating-classic-asp-to-asp-net-core/) --- ### **5. 示例:简单代码对比** #### **经典 ASP(VBScript)** ```asp <% Set conn = Server.CreateObject("ADODB.Connection") conn.Open "数据库连接字符串" Set rs = conn.Execute("SELECT * FROM Users") %> <ul> <% Do While Not rs.EOF %> <li><%= rs("Name") %></li> <% rs.MoveNext Loop %> </ul> ``` #### **ASP.NET Web Forms(C#)** ```csharp // 后台代码(.aspx.cs) protected void Page_Load(object sender, EventArgs e) { using (SqlConnection conn = new SqlConnection("连接字符串")) { SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Users", conn); DataTable dt = new DataTable(); da.Fill(dt); Repeater1.DataSource = dt; Repeater1.DataBind(); } } ``` ```html <!-- 前台页面(.aspx) --> <asp:Repeater ID="Repeater1" runat="server"> <ItemTemplate> <li><%# Eval("Name") %></li> </ItemTemplate> </asp:Repeater> ``` --- ### **总结** 迁移到 ASP.NET 可显著提升系统的可维护性、性能和安全性,但需根据项目规模选择合适的技术路径(如逐步迁移或全面重构)。建议从关键模块入手,逐步验证兼容性,并充分利用 .NET 生态的工具和框架简化开发流程。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值