1 花了點時間整理了一下ASP.NET Web.config配置文件的基本使用方法。很適合新手參看,因爲Web.config在使用很靈活,能夠自定義一些節點。因此這裏只介紹一些比較經常使用的節點。 2 3 <?xml version="1.0"?> 4 5 <!--注意: 除了手動編輯此文件之外,您還可使用 Web 管理工具來配置應用程序的設置。可使用 Visual Studio 中的「網站」->「Asp.Net 配置」選項。 6 7 設置和註釋的完整列表在 machine.config.comments 中,該文件一般位於 "Windows"Microsoft.Net"Framework"v2.x"Config 中。--> 8 9 <!--Webconfig文件是一個xml文件,configuration是xml文件的根節點,因爲xml文件的根節點只能有一個,因此Webconfig的全部配置都是在這個節點內進行的。--> 10 11 <configuration> 12 13 <!--指定配置節和命名空間聲明。clear:移除對繼承的節和節組的全部引用,只容許由當前 section 和 sectionGroup 元素添加的節和節組。remove:移除對繼承的節和節組的引用。 14 15 section:定義配置節處理程序與配置元素之間的關聯。sectionGroup:定義配置節處理程序與配置節之間的關聯。--> 16 17 <configSections> 18 19 <sectionGroup name="system.web.extensions" type="System.Web.Configuration.SystemWebExtensionsSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> 20 21 <sectionGroup name="scripting" type="System.Web.Configuration.ScriptingSectionGroup, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"> 22 23 <section name="scriptResourceHandler" type="System.Web.Configuration.ScriptingScriptResourceHandlerSection, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="false" allowDefinition="MachineToApplication"/> 24 25 </sectionGroup> 26 27 </sectionGroup> 28 29 <section name="rewriter" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter" /> 30 31 </configSections> 32 33 34 35 <!--appSettings是應用程序設置,能夠定義應用程序的全局常量設置等信息--> 36 37 <appSettings> 38 39 <add key="1" value="1" /> 40 41 <add key="gao" value="weipeng" /> 42 43 </appSettings> 44 45 46 47 <!--鏈接字符串設置--> 48 49 <connectionStrings> 50 51 <add name="ConnString" connectionString="Data Source=GAO;Initial Catalog=HBWXDate;User ID=sa;password=sa"></add> 52 53 <add name="111" connectionString="11111" /> 54 55 </connectionStrings> 56 57 58 59 <!--指定應用子配置設置的資源,並鎖定配置設置,以防止它們被子配置文件重寫。page指定應用包含的配置設置的資源.allowOverride是否容許配置文件的重寫,提升配置文件的安全性--> 60 61 <location path="Default.aspx" allowOverride="false"> 62 63 <!--控制asp.net運行時的行爲--> 64 65 <system.web> 66 67 <!--identity控制web應用程序的身份驗證標識.--> 68 69 <identity impersonate="false" /> 70 71 72 73 <!--標識特定於頁的配置設置(如是否啓用會話狀態、視圖狀態,是否檢測用戶的輸入等)。<pages>能夠在計算機、站點、應用程序和子目錄級別聲明. 74 75 這裏的幾個屬性的意思是默認主頁爲Index,主題是Default,不檢測用戶在瀏覽器輸入的內容中是否存在潛在的危險數據(注:該項默認是檢測,若是你使用了不檢測,一要對用戶的輸入進行編碼或驗證),在從客戶端回發頁時將檢查加密的視圖狀態,以驗證視圖狀態是否已在客戶端被篡改。(注:該項默認是不驗證)禁用ViewState--> 76 77 <pages masterPageFile="Index" theme="Default" buffer="true" enableViewStateMac="true" validateRequest="false" enableViewState="false"> 78 79 <!--controls 元素定義標記前綴所在的 register 指令和命名空間的集合--> 80 81 <controls></controls> 82 83 <!--將在程序集預編譯期間使用的導入指令的集合--> 84 85 <namespaces></namespaces> 86 87 </pages> 88 89 90 91 <!--默認錯誤頁設置,mode:具備On,Off,RemoteOnly 3種狀態。On表示始終顯示自定義的信息; Off表示始終顯示詳細的asp.net錯誤信息; RemoteOnly表示只對不在本地Web服務器上運行的用戶顯示自定義信息.defaultRedirect:用於出現錯誤時重定向的URL地址--> 92 93 <customErrors defaultRedirect="Err.html" mode="RemoteOnly"> 94 95 <!--特殊代碼編號的錯誤從定向文件--> 96 97 <error statusCode="403" redirect="NoAccess.htm" /> 98 99 <error statusCode="404" redirect="FileNotFound.htm" /> 100 101 </customErrors> 102 103 104 105 <!--配置調試和跟蹤:下面配置的意思是啓動調試(默認),捕獲跟蹤信息,要緩存的跟蹤請求個數(15),跟蹤結果的排列順序--> 106 107 <trace enabled="true" localOnly="false" pageOutput="true" requestLimit="15" traceMode="SortByCategory"/> 108 109 110 111 <!-- 設置 compilation debug="true" 將調試符號插入已編譯的頁面中。但因爲這會影響性能,所以只在開發過程當中將此值設置爲 true。設置默認的開發語言C#。batch是否支持批處理--> 112 113 <compilation debug="true" defaultLanguage="c#" batch="false"> 114 115 <assemblies> 116 117 <!--加的程序集引用,每添加一個程序集,就表示你的應用程序已經依賴了一個程序集,你就能夠在你的應用程序中使用了--> 118 119 <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> 120 121 <add assembly="System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> 122 123 <add assembly="System.Web.Extensions.Design, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 124 125 <add assembly="System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> 126 127 <add assembly="System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> 128 129 <add assembly="System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> 130 131 <add assembly="System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> 132 133 <add assembly="System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> 134 135 <add assembly="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/> 136 137 <add assembly="System.Web.Services, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> 138 139 <add assembly="System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/> 140 141 </assemblies> 142 143 <!--定義用於編譯自定義資源文件的生成提供程序的集合。--> 144 145 <buildProviders> 146 147 <!----> 148 149 <add extension=".aspx" type="System.Web.Compilation.PageBuildProvider"/> 150 151 <add extension=".ascx" type="System.Web.Compilation.UserControlBuildProvider"/> 152 153 <add extension=".master" type="System.Web.Compilation.MasterPageBuildProvider"/> 154 155 <add extension=".asmx" type="System.Web.Compilation.WebServiceBuildProvider"/> 156 157 <add extension=".ashx" type="System.Web.Compilation.WebHandlerBuildProvider"/> 158 159 <add extension=".soap" type="System.Web.Compilation.WebServiceBuildProvider"/> 160 161 <add extension=".resx" type="System.Web.Compilation.ResXBuildProvider"/> 162 163 <add extension=".resources" type="System.Web.Compilation.ResourcesBuildProvider"/> 164 165 <add extension=".wsdl" type="System.Web.Compilation.WsdlBuildProvider"/> 166 167 <add extension=".xsd" type="System.Web.Compilation.XsdBuildProvider"/> 168 169 <add extension=".rdlc" type="Microsoft.Reporting.RdlBuildProvider, Microsoft.ReportViewer.Common, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/> 170 171 </buildProviders> 172 173 </compilation> 174 175 176 177 <!--經過 <authentication> 節能夠配置 ASP.NET 使用的 安全身份驗證模式,以標識傳入的用戶。Windows: 使用IIS驗證方式,Forms: 使用基於窗體的驗證方式,Passport: 採用Passport cookie驗證模式,None: 不採用任何驗證方式--> 178 179 <authentication mode="Forms"> 180 181 <!--Name: 指定完成身份驗證的Http cookie的名稱.LoginUrl: 若是未經過驗證或超時後重定向的頁面URL,通常爲登陸頁面,讓用戶從新登陸。Protection: 指定 cookie數據的保護方式. 182 183 可設置爲:All表示加密數據,並進行有效性驗證兩種方式,None表示不保護Cookie,Encryption表示對Cookie內容進行加密,validation表示對Cookie內容進行有效性驗證,TimeOut: 指定Cookie的失效時間. 超時後要從新登陸。--> 184 185 <forms name=".ASPXUSERDEMO" loginUrl="Login.aspx" protection="All" timeout="30"/> 186 187 </authentication> 188 189 <!--控制對 URL 資源的客戶端訪問(如容許匿名用戶訪問)。此元素能夠在任何級別(計算機、站點、應用程序、子目錄或頁)上聲明。必需與<authentication> 節配合使用。此處的意思是對匿名用戶不進行身份驗證。拒絕用戶weipeng--> 190 191 <authorization> 192 193 <allow users="*"/> 194 195 <deny users="weipeng"/> 196 197 <allow users="aa" roles="aa" /> 198 199 </authorization> 200 201 <!--站點全球化設置,requestEncoding: 它用來檢查每個發來請求的編碼.responseEncoding: 用於檢查發回的響應內容編碼.fileEncoding:用於檢查aspx,asax等文件解析的默認編碼,默認的編碼是utf-8--> 202 203 <globalization requestEncoding="gb2312" responseEncoding="gb2312" fileEncoding="gb2312" /> 204 205 <!--會話狀態設置。mode: 分爲off,Inproc,StateServer,SqlServer幾種狀態 mode = InProc 存儲在進程中特色:具備最佳的性能,速度最快,但不能跨多臺服務器存儲共享.mode = "StateServer" 存儲在狀態服務器中特色:當須要跨服務器維護用戶會話信息時,使用此方法。可是信息存儲在狀態服務器上,一旦狀態服務器出現故障,信息將丟失. mode="SqlServer" 存儲在sql server中特色:工做負載會變大,但信息不會丟失 206 207 stateConnectionString :指定asp.net應用程序存儲遠程會話狀態的服務器名,默認爲本機。sqlConnectionString:當用會話狀態數據庫時,在這裏設置鏈接字符串。Cookieless:設置爲flase時,表示使用cookie會話狀態來標識客戶.timeout表示會話超時時間。--> 208 209 <sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="20"></sessionState> 210 211 <!--爲 ASP.NET 應用程序配置頁的視圖狀態設置。設置要存儲在頁歷史記錄中的項數。--> 212 213 <sessionPageState historySize="9"/> 214 215 <!--配置asp.net http運行庫的設置。能夠在計算機,站點,應用程序和子目錄級別聲明 216 217 容許最多的請求個數100,最長容許執行請求時間爲80秒,控制用戶上傳文件的大小,默認是4M。useFullyQualifiedRedirectUrl客戶端重定向不須要被自動轉換爲徹底限定格式。--> 218 219 <httpRuntime appRequestQueueLimit="100" executionTimeout="80" maxRequestLength="40960" useFullyQualifiedRedirectUrl="false"/> 220 221 <!--httpModules在一個應用程序內配置 HTTP 模塊。--> 222 223 <httpModules> 224 225 <add name="OutputCache" type="System.Web.Caching.OutputCacheModule" /> 226 227 <add name="Session" type="System.Web.SessionState.SessionStateModule" /> 228 229 <add name="WindowsAuthentication" type="System.Web.Security.WindowsAuthenticationModule" /> 230 231 <add name="FormsAuthentication" type="System.Web.Security.FormsAuthenticationModule" /> 232 233 <add name="PassportAuthentication" type="System.Web.Security.PassportAuthenticationModule" /> 234 235 <add name="RoleManager" type="System.Web.Security.RoleManagerModule" /> 236 237 <add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" /> 238 239 <add name="FileAuthorization" type="System.Web.Security.FileAuthorizationModule" /> 240 241 <add name="AnonymousIdentification" type="System.Web.Security.AnonymousIdentificationModule" /> 242 243 <!--自定義的URL重寫,type基本上就是dll名--> 244 245 <add name="UrlRewriter" type="Intelligencia.UrlRewriter.RewriterHttpModule, Intelligencia.UrlRewriter" /> 246 247 <add name="Profile" type="System.Web.Profile.ProfileModule" /> 248 249 </httpModules> 250 251 <!--httpHandlers用於根據用戶請求的URL和HTTP謂詞將用戶的請求交給相應的處理程序。能夠在配置級別的任何層次配置此節點,也就是說能夠針對某個特定目錄下指定的特殊文件進行特殊處理。 252 253 add:指定映射處處理程序的謂詞/路徑。clear:移除當前已配置或已繼承的全部處理程序映射。remove:移除映射處處理程序的謂詞/路徑。remove 指令必須與前一個 add 指令的謂詞/路徑組合徹底匹配。該指令不支持通配符。--> 254 255 <httpHandlers> 256 257 <remove verb="*" path="*.asmx"/> 258 259 <add verb="*" path="*.asmx" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> 260 261 <add verb="*" path="*_AppService.axd" validate="false" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> 262 263 <add verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" validate="false"/> 264 265 <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro.2"/> 266 267 </httpHandlers> 268 269 <!--爲 Web 應用程序使用的 Cookie 配置屬性。domain:設置 Cookie 域名。httpOnlyCookies:在 Internet Explorer 6 SP1 中啓用 HttpOnlyCookies Cookie 的輸出。默認值爲 false。requireSSL:獲取一個指示是否須要安全套接字層 (SSL) 通訊的值.--> 270 271 <httpCookies httpOnlyCookies="false" requireSSL="false"/> 272 273 <!--控制 ASP.NET Web 服務及其客戶端的行爲。protocols:指定傳輸協議,ASP.NET 可以使用這些傳輸協議來解密 HTTP--> 274 275 <webServices> 276 277 <protocols> 278 279 <add/> 280 281 </protocols> 282 283 </webServices> 284 285 <!--爲 Web 應用程序配置緩存設置。cache:定義全局應用程序緩存設置。outputCache :指定應用程序範圍的輸出緩存設置。outputCacheSettings:指定能夠應用於應用程序中頁的輸出緩存設置。sqlCacheDependency:爲 ASP.NET 應用程序配置 SQL 緩存依賴項。--> 286 287 <caching> 288 289 <cache disableMemoryCollection = "false" disableExpiration = "false" privateBytesLimit = "0" percentagePhysicalMemoryUsedLimit = "90" privateBytesPollTime = "00:02:00"/> 290 291 <!--設計須要以這種方式緩存的頁時,您須要向該頁添加如下指令:<%@ OutputCache CacheProfile="ServerOnly" %>--> 292 293 <outputCacheSettings> 294 295 <outputCacheProfiles> 296 297 <add name="ServerOnly" duration="60" varyByCustom="browser" location="Server" /> 298 299 </outputCacheProfiles> 300 301 </outputCacheSettings> 302 303 </caching> 304 305 </system.web> 306 307 </location> 308 309 <!--網絡設置,authenticationModules:指定用於對 Internet 請求進行身份驗證的模塊。connectionManagement:指定與 Internet 宿主的鏈接的最大數目。defaultProxy:配置超文本傳輸協議 (HTTP) 代理服務器。 310 311 mailSettings:配置簡單郵件傳輸協議 (SMTP) 郵件發送選項。requestCaching:控制網絡請求的緩存機制。settings:配置 System.Net 的基本網絡選項。--> 312 313 <system.net> 314 315 <!--配置SMTP電子郵件設置--> 316 317 <mailSettings> 318 319 <smtp from="weipeng"> 320 321 <network host="Gao" password="" userName="" /> 322 323 </smtp> 324 325 </mailSettings> 326 327 <!--禁用全部緩存--> 328 329 <requestCaching disableAllCaching="true"></requestCaching> 330 331 <!--指定代理地址,並對本地訪問和 contoso.com 跳過代理。--> 332 333 <defaultProxy> 334 335 <proxy usesystemdefault="True" proxyaddress="http://192.168.1.10:3128" bypassonlocal="True"/> 336 337 <bypasslist> 338 339 <add address="[a-z]+".contoso".com" /> 340 341 </bypasslist> 342 343 </defaultProxy> 344 345 </system.net> 346 347 <!--該節替換在 httpHandlers 和 httpModules 節中添加的與 AJAX 相關的 HTTP 處理程序和模塊。該節使 IIS 7.0 在集成模式下運行時可以使用這些處理程序和模塊。在iis7.0 下運行 ASP.NET AJAX 須要 system.webServer 348 349 節。對早期版本的 IIS 來講則不須要此節。 --> 350 351 <system.webServer> 352 353 <validation validateIntegratedModeConfiguration="false"/> 354 355 <modules> 356 357 <add name="ScriptModule" preCondition="integratedMode" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 358 359 </modules> 360 361 <handlers> 362 363 <remove name="WebServiceHandlerFactory-Integrated"/> 364 365 <add name="ScriptHandlerFactory" verb="*" path="*.asmx" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 366 367 <add name="ScriptHandlerFactoryAppServices" verb="*" path="*_AppService.axd" preCondition="integratedMode" type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 368 369 <add name="ScriptResource" preCondition="integratedMode" verb="GET,HEAD" path="ScriptResource.axd" type="System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/> 370 371 </handlers> 372 373 </system.webServer> 374 375 376 377 <!--ASP.NET AJAX 中配置 ASP.NET 服務--> 378 379 <system.web.extensions> 380 381 <!--配置 JSON 序列化--> 382 383 <scripting> 384 385 <webServices> 386 387 <jsonSerialization maxJsonLength="5000"/> 388 389 </webServices> 390 391 </scripting> 392 393 </system.web.extensions> 394 395 <!--對WCF的相關配置--> 396 397 <system.serviceModel> 398 399 <services> 400 401 <service name="WCFStudent.WCFStudentText" behaviorConfiguration="ServiceBehavior"> 402 403 <!-- Service Endpoints --> 404 405 <endpoint address="" binding="wsHttpBinding" contract="WCFStudent.IStuServiceContract"> 406 407 <!-- 部署時,應刪除或替換下列標識元素,以反映在其下運行部署服務的標識。刪除以後,WCF 將自動推導相應標識。--> 408 409 <identity> 410 411 <dns value="localhost"/> 412 413 </identity> 414 415 </endpoint> 416 417 <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/> 418 419 </service> 420 421 </services> 422 423 <behaviors> 424 425 <serviceBehaviors> 426 427 <behavior name="ServiceBehavior"> 428 429 <!-- 爲避免泄漏元數據信息,請在部署前將如下值設置爲 false 並刪除上面的元數據終結點 --> 430 431 <serviceMetadata httpGetEnabled="true"/> 432 433 <!-- 要接收故障異常詳細信息以進行調試,請將如下值設置爲 true。在部署前設置爲 false 以免泄漏異常信息--> 434 435 <serviceDebug includeExceptionDetailInFaults="false"/> 436 437 </behavior> 438 439 </serviceBehaviors> 440 441 </behaviors> 442 443 </system.serviceModel> 444 445 446 447 <!--URL重定向--> 448 449 <rewriter> 450 451 <rewrite url="~/user/u(.+).aspx" to="~/user/index.aspx?r=$1" /> 452 453 <rewrite url="~/ask/q(.+).aspx" to="~/home/ask/content.aspx?id=$1" /> 454 455 <rewrite url="~/blog/b(.+).aspx" to="~/home/blog/article.aspx?r=$1" /> 456 457 <rewrite url="~/news/n(.+).aspx" to="~/home/news/content.aspx?nid=$1" /> 458 459 <rewrite url="~/default.aspx" to="~/home/ram/net.aspx" /> 460 461 </rewriter> 462 463 </configuration>