web.config雖然一直用,接觸最多的也就是節點appSettings和connectionSettings,今天系統的梳理一下,了解一下webconfig各個節點的含義,先簡單的瀏覽一下具體的webcofig,然后在介紹一些必要的知識。
<?xml version="1.0" encoding="utf-8"?> <!--有時候webconfig中配置的節點較多,這時候可以將一些節點剝離出去單獨寫,然后引用,比如下面的<appSettings/>和<connectionStrings> --> <!-- For more information on how to configure your ASP.NET application, please visit http://go.microsoft.com/fwlink/?LinkId=301880 --> <!--configuraion是根節點,所有的配置內容都在configuration內進行--> <configuration> <!--指定配置節和命名空間聲明,配置節和配置節組定義在configSections--> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <!--連接字符串設置--> <connectionStrings configSource="connectionStrings.config" /> <!--appSettings是應用程序設置,可以定義應用程序的全局變量等信息--> <appSettings configSource="appSettings.config" /> <!--控制ASP.NET運行時的行為--> <system.web> <!--identity控制應用程序的身份驗證標識--> <identity impersonate="true" userName="Administrator" password="123" /> <!--通過 <authentication> 節可以配置 ASP.NET使用的安全身份驗證模式,以標識傳入的用戶。Windows: 使用IIS驗證方式, Forms: 使用基於窗體的驗證方式,Passport: 采用Passport cookie驗證模式,None: 不采用任何驗證方式 --> <authentication mode="Forms"> <forms name="MVCDemo" cookieless="UseCookies" loginUrl="~/Account/Auth/Login" protection="All" timeout="2880"/> <!--Name: 指定完成身份驗證的Http cookie的名稱。LoginUrl: 如果未通過驗證或超時后重定向的頁面URL,一般為登錄頁面,讓用戶重新登錄。 Protection: 指定 cookie數據的保護方式,可設置為:All表示加密數據並進行有效性驗證兩種方式,None表示不保護Cookie, Encryption表示對Cookie內容進行加密,validation表示對Cookie內容進行有效性驗證。TimeOut: 指定Cookie的失效時間,超時后要重新登錄。 --> </authentication> <!--默認錯誤頁設置.mode:具有On,Off,RemoteOnly 3種狀態。On表示始終顯示自定義的信息; Off表示始終顯示詳細的asp.net錯誤信息; RemoteOnly表示只對不在本地Web服務器上運行的用戶顯示自定義信息。defaultRedirect:用於出現錯誤時重定向的URL地址--> <customErrors defaultRedirect="Err.html" mode="RemoteOnly"> <!--特殊代碼編號的錯誤從定向文件--> <error statusCode="403" redirect="NoAccess.htm" /> <error statusCode="404" redirect="FileNotFound.htm" /> </customErrors> <!-- 設置 compilation debug="true" 將調試符號插入已編譯的頁面中。但由於這會影響性能,因此只在開發過程中將此值設置為 true。 設置默認的開發語言C#。batch是否支持批處理--> <compilation debug="true" defaultLanguage="c#" batch="false" targetFramework="4.5.2"> <assemblies> <!--加的程序集引用,每添加一個程序集,就表示你的應用程序已經依賴了一個程序集,你就可以在你的應用程序中使用了--> <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </assemblies> <!--定義用於編譯自定義資源文件的生成提供程序的集合。--> <buildProviders> <add extension=".aspx" type="System.Web.Compilation.PageBuildProvider"/> </buildProviders> </compilation> <pages validateRequest="false" controlRenderingCompatibilityVersion="5.0" enableViewState="false" enableSessionState="false" enableEventValidation="false"> <namespaces> <add namespace="System.Web.Helpers" /> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Routing" /> <add namespace="System.Web.WebPages" /> </namespaces> </pages> <!--配置asp.net http運行庫的設置。可以在計算機,站點,應用程序和子目錄級別聲明 允許最多的請求個數100,最長允許執行請求時間為80秒,控制用戶上傳文件的大小,默認是4M。 useFullyQualifiedRedirectUrl客戶端重定向不需要被自動轉換為完全限定格式。--> <httpRuntime appRequestQueueLimit="100" executionTimeout="80" maxRequestLength="40960" useFullyQualifiedRedirectUrl="false" targetFramework="4.5.2" /> <!--httpModules在一個應用程序內配置 HTTP 模塊。--> <httpModules> <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" /> </httpModules> <!--為 Web 應用程序配置緩存設置。cache:定義全局應用程序緩存設置。outputCache :指定應用程序范圍的輸出緩存設置。 outputCacheSettings:指定可以應用於應用程序中頁的輸出緩存設置。sqlCacheDependency:為 ASP.NET 應用程序配置 SQL 緩存依賴項。--> <caching> <cache disableMemoryCollection = "false" disableExpiration = "false" privateBytesLimit = "0" percentagePhysicalMemoryUsedLimit = "90" privateBytesPollTime = "00:02:00"/> <!--設計需要以這種方式緩存的頁時,您需要向該頁添加以下指令:<%@ OutputCache CacheProfile="ServerOnly" %>--> <outputCacheSettings> <outputCacheProfiles> <add name="ServerOnly" duration="60" varyByCustom="browser" location="Server" /> </outputCacheProfiles> </outputCacheSettings> </caching> </system.web> <!--該節替換在 httpHandlers 和 httpModules 節中添加的與 AJAX 相關的 HTTP 處理程序和模塊。 該節使 IIS 7.0 在集成模式下運行時可使用這些處理程序和模塊。在iis7.0 下運行 ASP.NET AJAX 需要 system.webServer 節。 對早期版本的 IIS 來說則不需要此節。--> <system.webServer> <modules> <remove name="FormsAuthentication" /> <remove name="ApplicationInsightsWebTracking" /> <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" /> </modules> <validation validateIntegratedModeConfiguration="false" /> </system.webServer> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" /> <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0" /> </dependentAssembly> <dependentAssembly> <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="mssqllocaldb" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework> <system.codedom> <compilers> <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" /> <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+" /> </compilers> </system.codedom> </configuration>
上邊我們已經把具體的webconfig內容詳細介紹了一遍,上邊也說了,有時候webconfig中配置的節點較多,這時候可以將一些節點剝離出去單獨寫,然后引用,比如<appSettings/>和<connectionStrings>
剝離出來的<appSettings/>文件如下:

<?xml version="1.0" encoding="utf-8"?> <appSettings > <!--RabbitMQ服務[Begin]--> <add key="RabbitMQHostUri" value="tcp://127.0.0.1:20001/RabbitMQHost" /> <!--RabbitMQ服務[End]--> <add key="webpages:Version" value="3.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="PreserveLoginUrl" value="true" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> <add key="WebSiteSqlServerDAL" value="MVC.DB.SqlServer.DAL" /> </appSettings>
剝離出來的<connectionStrings>文件如下:

<?xml version="1.0" encoding="utf-8"?> <connectionStrings> <clear /> <add name="MvcDataConstr" connectionString="Server=.;Initial Catalog=MvcData;User ID=sa;Password=123" providerName="System.Data.SqlClient" /> </connectionStrings>
附張圖吧,其實位置都無所聞,只要指定好就行