ASP.NET MVC 4中如何读取web.config中的配置
2016-02-10 · 百度知道合伙人官方认证企业
web.config内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation defaultLanguage="c#" debug="true" />
</system.web>
<appSettings>
<add key="ConnectionInfo" value="server=(local);database=Northwind;Integrated Security=SSPI" />
</appSettings>
</configuration>
读取配置的方法:
void Page_Load(object sender, EventArgs e)
{
string connectionInfo = ConfigurationSettings.AppSettings["ConnectionInfo"];
using(SqlConnection connection = new SqlConnection(connectionInfo))
{
connection.Open();
// perform work with connection
}
}
用ConfigurationManager这个类。
ASP.NET MVC 4中读取web.config中的配置方法如下:
1、web.config内容:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation defaultLanguage="c#" debug="true" />
</system.web>
<appSettings>
<add key="ConnectionInfo" value="server=(local);database=Northwind;Integrated Security=SSPI" />
</appSettings>
</configuration>
2、读取配置的方法:
void Page_Load(object sender, EventArgs e)
{
string connectionInfo = ConfigurationSettings.AppSettings["ConnectionInfo"];
using(SqlConnection connection = new SqlConnection(connectionInfo))
{
connection.Open();
// perform work with connection
}
}
<add key="x" value="y">
</appSetting>
然后CS中
string x=ConfigurationManager.AppSettings["x"];
我把配置信息写全局的web.config文件中,然后用
System.Web.Configuration.WebConfigurationManager.AppSettings["name"];是可以读取出来。
但是不明白Views目录下的web.config是怎么用的?
Views下的只管Views的配置,其实一般也很少用到。你使用之前在View上应该要引用ConfigurationManager的空间名称