您的位置:首页 > 其它

为 IIS 7.0 配置 <system.webServer>

2014-12-06 11:26 477 查看
Web.config 文件中的 system.webServer 节用于指定适用于 Web 应用程序的 IIS 7.0 设置。system.WebServer 是 configuration 节的子级。有关更多信息,请参见 IIS 7.0: system.webServer Section Group (IIS Settings Schema)(IIS 7.0:system.webServer 节组(IIS 设置架构))。

下面是可以在 system.WebServer 配置组中进行的 Web 服务器设置的示例:

当请求未包含特定资源时,Web 服务器返回给客户端的默认文档(defaultDocument 元素)。

响应的压缩设置(httpCompression 元素)。

自定义标头(httpProtocol 节的 customHeaders 元素)。

模块(modules 元素)。

处理程序(handlers 元素)。

system.webServer 节中的某些设置只适用于 IIS 7.0 集成模式,而不适用于经典模式。具体而言,如果应用程序正在经典模式下运行,则会忽略 Web.config 文件的 system.WebServer节中指定的所有托管代码模块和处理程序。与 IIS 的早期版本相同,托管代码模块和处理程序必须在 system.web 节的 httpModuleshttpHandlers 元素中定义。

本主题阐释需要修改 system.webServer 节的三个常见配置任务:

添加默认文件,以便在请求 URL 未包含特定的文件时,提供该默认文件。

注册托管代码模块。

添加自定义响应标头。

配置默认文件

当请求 URL 未包含 Web 应用程序的特定文件时,IIS 7.0 将提供一个默认文件。

配置默认文件

如果应用程序没有 Web.config 文件,请使用 Visual Studio 或文本编辑器创建该文件。

有关更多信息,请参见编辑 ASP.NET 配置文件

如果 Web.config 文件尚未包含 system.webServer 节,请在 configuration 元素中创建该节,如下面的示例所示:

<configuration>
<system.webServer>
</system.webServer>
</configuration>


在 system.webServer 元素内,创建一个 defaultDocument 元素。

在 defaultDocument 元素内,创建一个 files 元素。

在 files 元素内创建一个 add 元素,并在 value 属性内指定默认文件的路径和名称。

下面的示例演示了一个 system.webServer 节,该节配置为提供 Products.aspx 文件作为默认文件。

<configuration>
<system.webServer>
<defaultDocument>      <files>        <add value="Products.aspx" />      </files>    </defaultDocument>
</system.webServer>
</configuration>


注册托管代码模块

每次请求时都会调用托管代码模块,通过该模块可对请求或响应进行自定义。

配置自定义托管代码模块

如果应用程序没有 Web.config 文件,请使用 Visual Studio 或文本编辑器创建该文件。

有关更多信息,请参见编辑 ASP.NET 配置文件

如果 Web.config 文件尚未包含 system.webServer 节,请在 configuration 元素中创建该节,如下面的示例所示:

<configuration>
<system.webServer>
</system.webServer>
</configuration>


在 system.webServer 元素内,创建一个 modules 元素。

在 modules 元素内创建一个 add 元素,并在 name 和 type 属性中指定自定义模块。

实际的名称和类型取决于要添加的模块。下面的示例演示如何添加名为 CustomModule 的自定义模块,该模块将实现为类型 Samples.CustomModule。

<configuration>
<system.webServer>
<modules>      <add name="CustomModule" type="Samples.CustomModule" />    </modules>
</system.webServer>
</configuration>


向模块注册中添加 precondition 属性,并将其值设置为 managedHandler。

此前置条件会导致仅在请求 ASP.NET 应用程序资源(例如 .aspx 文件或托管处理程序)时才调用该模块。该资源中不包括静态文件(例如 .htm 文件)。

其 configuration 节将类似于以下示例。



<configuration>
<system.webServer>
<modules>
<add name="CustomModule" type="Samples.CustomModule"
precondition="managedHandler" />
</modules>
<defaultDocument>
<files>
<add value="Products.aspx" />
</files>
</defaultDocument>
</system.webServer>
</configuration>


配置自定义响应标头

利用自定义响应标头,可向浏览器发送应用程序特定的信息。例如,可以添加 Content-Language 标头来描述网页正文中使用的语言。若要执行此操作,请提供一个或多个语言和国家/地区值,例如 en-US(美国英语)或 en-GB(英国英语)。

配置自定义响应标头

如果应用程序没有 Web.config 文件,请使用 Visual Studio 或文本编辑器创建该文件。

有关更多信息,请参见编辑 ASP.NET 配置文件

如果 Web.config 文件尚未包含 system.webServer 节,请在 configuration 元素中创建该节,如下面的示例所示:

<configuration>
<system.webServer>
</system.webServer>
</configuration>


在 system.webServer 元素内,创建一个 httpProtocol 元素。

在 httpProtocol 元素内,创建一个 customHeaders 元素。

在 customHeaders 元素内创建一个 add 标记,并在 name 和 value 属性中指定自定义标头。

实际的名称和类型将取决于该标头在应用程序中的功能。下面的示例演示如何添加名为 CustomHeader 且值为 CustomHeader 的自定义标头。

<configuration>
<system.webServer>
<httpProtocol>      <customHeaders>
<add name="CustomHeader" value="CustomHeader" />
<customHeaders>
</httpProtocol>
</system.webServer>
</configuration>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐