1. 程式人生 > >ASP.NET基礎知識彙總之WebConfig各節點介紹

ASP.NET基礎知識彙總之WebConfig各節點介紹

  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=\&quot;Web\&quot; /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>
View Code

 

  剝離出來的<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>
View Code

 

附張圖吧,其實位置都無所聞,只要指定好就行