据说可能是介绍 web.config 最详细的文章。大家参考参考[转]

web|参考

Web.Config  Written on: Nov, 16th 2001.
Application("DSN") = "Server=moon; Driver=Sql Server; Database=Store; UID=user; PWD=bingo;"
Above declaration in the global.asa file might be familiar to almost all ASP programmers.

While going through the MSDN, I was overwhelmed, by looking into the web.config file which handles all configuration for an application. The replacement for the above declaration in ASP .NET is as follows:

<configuration>
<appSettings>
<add key="DSN" value="Server=moon;database=Store;Trusted_Connection=yes" />
</appSettings>
</configuration>

Then, in your ASPX page, you should have the following statement to retrieve the value for DSN.

Dim dsn As String = ConfigurationSettings.AppSettings("DSN")

So, I started to ask the following questions to myself.

What exactly is web.config?
Does this handles only the above example?
What are the benefits of web.config?

And, following were the results for my questions, and I would like to share with you all. This is based on Beta2

Introduction

Well, web.config is a XML-based configuration file. If you see the above example, you can make sure that all the elements are based on XML standards. Obviously, we can develop a tool for modifying and editing this configuration file.

A web.config can appear in any directory on an ASP.NET Web application server. Said this, if you have a web.config file in the directory "c:\inetpub\wwwroot", then the settings specified in the web.config is applicable to all the subdirectories under wwwroot. Each sub-directory can have its own web.config file and it will overwrite the settings of the web.config file in the parent directory.

There is another file called machine.config, which provides configuration settings for the entire server. If you change the contents of any web.config file then the change will be immediately reflected in the processing of any incoming requests to the web' server. These settings are calculated only once and then cached across subsequent requests. ASP.NET automatically watches for file changes and will invalidate the cache if any of the configuration files change. (For more information on caching Click here)

The root element of a web.config file is always a <configuration> tag. The <configuration> tag contains three different types of elements: 1) configuration section handler declarations, 2) configuration section groups, and 3) configuration section settings.

Following are the list of commonly used Configuation tags, that, we be used in our web applications and will go thru them

1) Appsettings
2) Authentication
3) Authorization
4) Compilation
5) CustomErrors
6) Globalization
7) Identity
8) MachineKey
9) Pages
10) ProcessModel
11) SessionState
12) Trace

<appSettings>
This can be declared at the machine, site, application and subdirectory level Include all the custom settings for your application in this section. Appsettings tag contains two attributes viz; key and value.

<add key="key" value="value"/>
Eg: <add key="DSN" value="Server=moon;database=Store;Trusted_Connection=yes" />

<authentication>
All the authentication/security related stuff are declared in this section. Authentication section contains a single attribute called "mode". Possible values for "mode" are (a) Forms (b) None (c) Passport and (d) Windows

Form based authentication can be used, if you want to use ASP .NET forms-based authentication.

If you want to allow anyonmyous users to access your website, select none.

Passpost authentication can be used, if you want the authentication to be based on Microsoft Passport authentication mode.

Use windows mode authentication, if you want to use Basic, Digest, Integrated Windows authentication (NTLM/Kerberos), or certificates

Note: If you are using Form based authentication, then you have several other options such as how the password should be encrypted, while submitting the form, if login fails, which page should be shown to the user etc.

As the AuthenTication is included in, System.Web.Configuration.AuthenticationConfigHandler while setting the authentication mode, you should code as follows

Eg:
<configuration>
<system.web>
<authentication mode="None" />
</system.web>
</configuration>

<authorization>
This is a very powerful tag, were you can restrict or allow users who wish to visit your web site. Authorization tag contains two sub tags such as allow and deny.

Allow tag provides us with three attributes, namely users, roles and verbs. We can add the list of users seperated by comma in the users attribute. Also we can specify the role in which each user belongs too. Important aspect of the attribute verb is that, we can control users depending upon the web request that the server is getting. The verb attribute provides us with four options GET, HEAD, POST and DEBUG.

Deny tag has the same attributes as the allow tag has. Other aspect of both these tags are, we can use two special symbols ? and * to specify anonymous users and "all users" respectively.

Eg:
<configuration>
<system.web>
<authorization>
<allow roles="Admins" />
<deny users="*" />
</authorization>
</system.web>
</configuration>

<compilation>
It is in this tag, you set all your compilcation options. This tag contains three sub-tags and seven attributes, which are discussed below.

Attributes
debug specifies whether to compile retail binaries or debug binaries. True specifies debug binaries and False specifies Retail binaries

defaultLanguage can be used to specify the language names to use in dynamic compilation files.

use explicit attribute to turn on explicit option or to turn off. This takes either true or false, were true means explicit is enabled.

We can also do a batch compiliation by specifying the attribute bath as true. If we have batch compiliation, then we might face the timeout problem. Then we may also want to use the batchTimeout attribute to set the time for batch timeout.

numRecompilesBeforeApprestart is the next attribute. This attribute indicates the number of dynamic recompiles of resources that can occur before the application restarts. This attribute is supported at the global and application level but not at the directory level.

Strict attribute indicates the settings of the visual basic strict compile option. supports two values, TRUE and FALSE.

SubTags
Compilers tag contains many or one compiler tag, were we define new compiler options. Assemblies and Namespaces specifies ASP .NET processing directives

Eg:
<configuration>
<system.web>
<compilation defaultLanguage="VB" debug="true">
<compilers>
<compiler language="VB;VBScript" extension=".cls" type="Microsoft.VB. VBCodeProvider,System" />
<compiler language="C#;Csharp" extension=".cs" type="Microsoft.CSharp. CSharpCodeProvider,System" />
</compilers>
<assemblies>
<add assembly="ADODB" />
<add assembly="*" />
</assemblies>
<namespaces>
<add namespace="System.Web" />
<add namespace="System.Web.UI" />
<add namespace="System.Web.UI.WebControls" />
<add namespace="System.Web.UI.HtmlControls" />
</namespaces>
</compilation>
</system.web>
</configuration>

<customErrors>
As the name says all about, customErros provides information about custom error messages for an ASP.NET application. CustomErrors tag provides us with three attributes.

defaultRedirect can be used to specify the URL to direct a browser, if any unexpected error occurs. The mode attribute takes three values On, Off or RemoteOnly. Remeteonly specifies that custom errors are shown only to remote clients.

The subtag <error> might be very useful in a variety of way. We can specify the error status code and ask the browser to redirect to a specific page. We should use the attribute, statusCode to specify the error status code and the redirect attribute to specify the redirect URL.

Eg:
<configuration>
<system.web>
<customErrors defaultRedirect="error.aspx" mode="RemoteOnly">
<error statusCode="500" redirect="InternalError.htm"/>
</customErrors>
</system.web>
</configuration>

<globalization>
Configures the globalization settings of an application. Two important attributes of this tag are requestEncoding and responseEncoding. Default values for both encoding are "iso-8859-1", which is English.

Eg:
<configuration>
<system.web>
<globalization requestEncoding="iso-8859-1" responseEncoding="iso-8859-1">
<globalization/>
</system.web>
</configuration>

<identity>
Controls the application identity of the Web application. Supports three attributes. Impersonate is the first attribute, which specifies whether client impersonation is used on each request to the web server. Takes either TRUE or FALSE. If the impersonation is FALSE, then we should specify the values for the attributes, username and password.

Eg:
<configuration>
<system.web>
<identity impersonate="true" />
</system.web>
</configuration>

<machineKey>
Configures keys to use for encryption and decryption of Forms authentication cookie data. This section can be declared at the machine, site, and application levels but not at the subdirectory level. This tag supports three attributes; validationKey, decryptionKey and validation.

ValidationKey and DecryptionKey takes the default value, which is AutoGenerate. We can also specify a key and it should be length of 128 hexadecimal characters. The validation attribute can be used to specify the alogrithm to be used while encryption. Possible values are SHA1, MD5 and 3DES.

<pages>
As the name indicates, we should use this tag to specify the page-specific configuration settings. It supports six attributes. We will dicsuss each one of them.

Buffer attribute specifies, whether resources are buffered or not. This takes three values On, Off and Readonly.

We can enable the session state or disable the session by using the attribute, enableSessionState. This takes two values, either TRUE or FALSE.

pageBaseType can be used to specify code-behind class that an .aspx page inherits. userControlBaseType specifies a code behind class that UserControls inherit.

If you want to disable any event firing in the page, you can use the attribute autoEventWireup. This too takes either TRUE or FALSE.

Eg:
<configuration>
<system.web>
<pages buffer="true" enableSessionState="true" autoEventWireup="true">
</pages>
</system.web>
</configuration>

<processModel>
This section is mainly for the Web Administrators. We should use this tag responsibly. We can use use tag to specify the timeout for when a new worker process should start in place of current one, the idleTimeout which specifies the minutes that ASP .NET automatically shuts down the worker process. One of the important attribute of this tag is requestQueueLimit, were you can specify the number of requests allowed in the queue before ASP .NET begins returning "503" (Server too busy error). Default is 5000.

Eg:
<configuration>
<system.web>
<processModel enable="true" timeout="10" idleTimeout="20" requestQueueLimit="100">
</processModel>
</system.web>
</configuration>

<sessionState>
This tag can be used to specify, were we are storing the session. This can be specified in the mode attribute. Supported values mode are Off, InProc, StateServer and SqlServer. InProc indicates that, session states is stored locally. StateServer indicates that session state is stored on a remote server and sqlserver can be used to indicate that the session state is stored on a sql server.

We also have the choice to use cookies to store the sessions. This can be set using the attribute cookieless. Session timeout can be specified using the attribute called timeout. By default, the session timeout is 20 minutes (same as classic ASP).

Eg:
<configuration>
<system.web>
<sessionState mode="Inproc" cookieless="true" timeout="20">
</sessionState>
</system.web>
</configuration>

<trace>
This is a very useful tag to debug our programs. We can use the trace tag to show all the information for the page processed by the server. By default, all the traces are stored on the server. We can specify the number of traces stored in the memory by using the attribute called requestLimit. Default is 10. We can either append the trace to the page or can be viewed using the trace utility. This is specified by the attribute called pageOutput.

Eg:
<configuration>
<system.web>
<trace enabled="false" requestLimit="15" pageOutput="true">
</trace>
<system.web>
</configuration>

There are some more tags available which can be used in the web.config file. Those are <httpHandlers>, <httpModules>, <httpRuntime>, <securityPolicy>, <webServices>, <trust> and <browserCaps>. You may want to look into these.

Summary
That was a small introduction for web.config file. And to end with, I have two tips for you.

Suppose, if we are creating a new folder and if we want to override the configuration settings of the parent folder, what we have to do is just create another web.config file in the sub-directory. If we need to prevent the overriding of the new web.config file in the subdirectory, then we can add the attribute allowOverride in the location tag. Also, we can specify the application name in the attribute path.

<configuration>
<location path="app1" allowOverride="false">
<system.web>
<identity impersonate="false" userName="app1" password="app1pw" />
</system.web>
</location>
</configuration>

What if some one types the web.config file in the URL?

ASP.NET configures IIS to prevent direct browser access to web.config files to ensure that their values cannot become public (attempts to access them will cause ASP.NET to return 403: Access Forbidden).

External Links
http://www.123aspx.com/directory.aspx?dir=85
http://msdn.microsoft.com/library/en-us/cpguidnf/html/cpconcreatingnewsectionhandlers.asp

时间: 2024-08-08 03:58:20

据说可能是介绍 web.config 最详细的文章。大家参考参考[转]的相关文章

web.config配置详细说明

(一).Web.Config是以XML文件规范存储,配置文件分为以下格式     1.配置节处理程序声明     特点: 位于配置文件的顶部,包含在<configSections>标志中.     2.特定应用程序配置     特点: 位于<appSetting>中. 可以定义应用程序的全局常量设置等信息.     3.配置节设置     特点: 位于<system.Web>节中,控制Asp.net运行时的行为.     4.配置节组     特点: 用<sect

asp.net-Asp.net网站上传,web.config

问题描述 Asp.net网站上传,web.config 我在网上买了域名和空间,想建个人网站,准备用学习asp.net,用vs2010,写了一个很简单的网站,调试的时候很正常,但是上传到虚拟主机之后,无法访问,说是web.config的问题,这个web.config该怎么弄呢,我写的时候什么都没加. 解决方案 ASP.NET web.config 上传配置说明asp.net通过web.config设置网站默认页ASP.NET C# 的Web.config文件详细介绍 解决方案二: 把具体的错误信

ASP.NET中Web.config文件的层次关系详细介绍

Web.config 是一个基于 XML 的配置文件,该文件的作用是对应用程序进行配置,下面为大家介绍下ASP.NET中Web.config文件的层次关系 Web.config 是一个基于 XML 的配置文件,该文件的作用是对应用程序进行配置,比如规定客户的认证方法,基于角色的安全技术的策略,数据绑 定的方法,远程处理对象等. 可以在网站的根目录和子目录下分别建立自己的 Web.config 文件,也可以一个Web.config 文件都不建立,Web.config 并不是网站必备的文件.这是因为

ASP.NET中Web.config文件的层次关系详细介绍_实用技巧

Web.config 是一个基于 XML 的配置文件,该文件的作用是对应用程序进行配置,比如规定客户的认证方法,基于角色的安全技术的策略,数据绑 定的方法,远程处理对象等. 可以在网站的根目录和子目录下分别建立自己的 Web.config 文件,也可以一个Web.config 文件都不建立,Web.config 并不是网站必备的文件.这是因为服务器有一个总 的配置文件,名为"Machine.config" ,默认安装在"C:\Windows\Microsoft.NET\ Fr

web.config 简单介绍

web 叫做web.config当然就是配置网站用的啦 很多东西都可以在这里设置下面简单介绍一下web.config是一个xml文档(现在越来越流行用xml做配置文件了)根元素是configuration 然后包含一个system.web节点 在第三层次是对站点的各种设置web.config可以设置的标签非常之多,那么,简单的做个介绍,当用到的时候可以去好好查阅在web系统中自定义的设置 我经常使用它来保存数据库的一些相关连接数据 之后读取很方便关于浏览器的设置 比如是否允许JavaApplet

.NET重要文件——Web.config

web Web.config文件是一个XML文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NET Web 应用程序的身份验证方式),它可以出现在应用程序的每一个目录中.当你通过Visual Studio.NET新建一个Web应用程序后,默认情况下会在根目录自动创建一个默认的Web.config文件,包括默认的配置设置,所有的子目录都继承它的配置设置.如果你想修改子目录的配置设置,你可以在该子目录下新建一个Web.config文件.它可以提供除从父目录继承的配

.Net网站的web.config配置说明

 一.认识Web.config文件 Web.config 文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NET Web 应用程序的身份验证方式),它可以出现在应用程序的每一个目录中.当你通过.NET新建一个Web应用程序后,默认情况下会在根目录自动创建一个默认的Web.config文件,包括默认的配置设置,所有的子目录都继承它的配置设置.如果你想修改子目录的配置设置,你可以在该子目录下新建一个Web.config文件.它可以提供除从父目录

DotText源码学习——从配置文件Web.config入手(一)

概述 ASP.NET配置数据存储在名为 Machine.config/Web.config的XML文本文件中,Web.config文件可以出现在ASP.NET应用程序的多个目录中.由于 这些文件将应用程序配置设置与应用程序代码分开,可以方便地设置与应用程序关联.正是因为配置文件中存储着关于整个应用程序的设置,当我读一个陌生项目的 源码时,经常把它作为入口. 我将从以下几点分析ASP.NET配置文件: ASP.NET配置文件的层次结构 Machine.config和根Web.config配置文件

Web.config详解

一.认识Web.config文件 Web.config 文件是一个XML文本文件,它用来储存 ASP.NET Web 应用程序的配置信息(如最常用的设置ASP.NET Web 应用程序的身份验证方式),它可以出现在应用程序的每一个目录中.当你通过.NET新建一个Web应用程序后,默认情况下会在根目录自动创建一个默认的Web.config文件,包括默认的配置设置,所有的子目录都继承它的配置设置.如果你想修改子目录的配置设置,你可以在该子目录下新建一个Web.config文件.它可以提供除从父目录继