ASP.NET MVC 4中读取web.config中的配置方法如下: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} }
public static class ConfigHelper{public static readonly string ConnStrScO2O = ConfigurationManager.ConnectionStrings["ScO2O"].ConnectionString;}要引用dll还有添加命名空间view中的webconfig是通常添加一些公共的命名空间,这样就不用每个view都独自using了