前言
对于项目配置文件的读取和修改,.net 提供了ConfigurationManager(位于System.Configuration命名空间) 和WebConfigurationManager(位于System.Web.Configuration命名空间)两个类,能够很方便的操作项目的配置文件。另外,对于 Web 应用程序配置,建议使用 WebConfigurationManager 类,而不要使用 ConfigurationManager 类。
演示
下面是一个 mvc5项目的项目配置文件
Web.config
1 <?xml version="1.0" encoding="utf-8"?> 2 <!-- 3 有关如何配置 应用程序的详细信息,请访问 4 /fwlink/?LinkId=301880 5 --> 6 <configuration> 7 <configSections> 8<!-- For more information on Entity Framework configuration, visit /fwlink/?LinkID=237468 --> 9<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />10 </configSections>11 <connectionStrings>12<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApplication1-0513023153.mdf;Initial Catalog=aspnet-WebApplication1-0513023153;Integrated Security=True" providerName="System.Data.SqlClient" />13<add name="MoocEntities" connectionString="metadata=res://*/Models.MOOC.csdl|res://*/Models.MOOC.ssdl|res://*/Models.MOOC.msl;provider=System.Data.SqlClient;provider connection string="data source=.;initial catalog=Mooc;user id=sa;password=aaaaaa;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />14 </connectionStrings>15 <appSettings>16<add key="webpages:Version" value="3.0.0.0" />17<add key="webpages:Enabled" value="false" />18<add key="ClientValidationEnabled" value="true" />19<add key="UnobtrusiveJavaScriptEnabled" value="true" />20 </appSettings>21 <system.web>22<authentication mode="None" />23<compilation debug="true" targetFramework="4.5" />24<httpRuntime targetFramework="4.5" />25 </system.web>26 <system.webServer>27<modules>28 <remove name="FormsAuthenticationModule" />29</modules>30 </system.webServer>31 <runtime>32<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">33 <dependentAssembly>34 <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />35 <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />36 </dependentAssembly>37 <dependentAssembly>38 <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />39 <bindingRedirect oldVersion="1.0.0.0-5.0.0.0" newVersion="5.0.0.0" />40 </dependentAssembly>41 <dependentAssembly>42 <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />43 <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />44 </dependentAssembly>45 <dependentAssembly>46 <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />47 <bindingRedirect oldVersion="1.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />48 </dependentAssembly>49</assemblyBinding>50 </runtime>51 <entityFramework>52<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">53 <parameters>54 <parameter value="v11.0" />55 </parameters>56</defaultConnectionFactory>57<providers>58 <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />59</providers>60 </entityFramework>61 </configuration>
获取与修改appSettings配置节点下的属性值
WebConfigurationManager提供了AppSettings属性,便于访问配置文件中appSettings节点下的元素的value值
下图为Web.config中要获取值的元素
编写如下代码,并在第27行打上断点调试
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using System.Web.Configuration; 7 using System.Text; 8 using System.Collections.Specialized; 9 using System.Collections;10 using System.Diagnostics;11 namespace WebApplication1.Controllers12 {13public class HomeController : Controller14{15 public ActionResult Index()16 {17 18 string version = WebConfigurationManager.AppSettings["webpages:Version"];19 string enabled = WebConfigurationManager.AppSettings["webpages:Enabled"];20 string clientValidationEnabled = WebConfigurationManager.AppSettings["ClientValidationEnabled"];21 string unobtrusiveJavaScriptEnabled = WebConfigurationManager.AppSettings["UnobtrusiveJavaScriptEnabled"];22 Debug.WriteLine("配置信息如下");23 Debug.WriteLine(version);24 Debug.WriteLine(enabled);25 Debug.WriteLine(clientValidationEnabled);26 Debug.WriteLine(unobtrusiveJavaScriptEnabled);27 return Content("");28 }29 30}31 }
调试模式“输出”窗口
以WebConfigurationManager.AppSettings["ClientValidationEnabled"]="要修改的值"可直接修改值
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using System.Web.Configuration; 7 using System.Text; 8 using System.Collections.Specialized; 9 using System.Collections;10 using System.Diagnostics;11 namespace WebApplication1.Controllers12 {13public class HomeController : Controller14{15 public ActionResult Index()16 {17 18 string version = WebConfigurationManager.AppSettings["webpages:Version"];19 string enabled = WebConfigurationManager.AppSettings["webpages:Enabled"];20 WebConfigurationManager.AppSettings["ClientValidationEnabled"]="aaaaaaaaaaaaaaaaaaaaa";//动态修改value21 string clientValidationEnabled = WebConfigurationManager.AppSettings["ClientValidationEnabled"];22 string unobtrusiveJavaScriptEnabled = WebConfigurationManager.AppSettings["UnobtrusiveJavaScriptEnabled"];23 Debug.WriteLine("配置信息如下");24 Debug.WriteLine(version);25 Debug.WriteLine(enabled);26 Debug.WriteLine(clientValidationEnabled);27 Debug.WriteLine(unobtrusiveJavaScriptEnabled);28 return Content("");29 }30 31}32 }
调试输出
连接字符串修改
1 /*获取数据库连接字符串*/2 string moocConnection = WebConfigurationManager.ConnectionStrings["MoocEntities"].ConnectionString;3 Debug.WriteLine("连接字符串如下:");//打印4 Debug.WriteLine(moocConnection);//打印
未完待续-----