vivion34 发表于 2017-12-28 16:03:31

Web.config设置system.webServer

  在 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>
  


 关于MVC
  对于像MVC这种比较特殊的URL,例如www.store.com/books/GetById/2
  因为没有文件后缀名,IIS通常会无法解析,返回403或者404错误。
  ASP.NET v4.0增加了新的特性,当运行在IIS7以上版本,并且需要IIS的一个快速修复程序KB980368,配置web.config后,将会正常处理上面这种 extensionless URL。
  <validation validateIntegratedModeConfiguration="false"/>这个主要作用是设置不检测 <system.web>中的配置 。
  <modules runAllManagedModulesForAllRequests="true" /> 这里当设置为true的时候,所有的请求,包含静态文件的请求和没有文件扩展名的请求,都会经过自定义的HttpModule,其实就是为了伪静态。
  IIS经典模式下的配置
  服务器会继续通过 Aspnet_isapi.dll 路由托管代码请求,其处理请求的方式就像应用程序在 IIS 6.0 中运行一样,aspnet_isapi.dll(IIS的native handler扩展),通过映射到System.Web.DefaultHttpHandler进行处理。
  

<system.webServer>  
<validation validateIntegratedModeConfiguration="false" />
  
<modules runAllManagedModulesForAllRequests="true" />
  
<handlers>
  
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
  
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
  
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
  
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
  
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  
</handlers>
  
</system.webServer>
  

  IIS集成模式下的配置
  服务器将使用 IIS 和 ASP.NET 的集成请求处理管道来处理请求,映射到System.Web.Handlers.TransferRequestHandle来处理
  

<modules runAllManagedModulesForAllRequests="true" />  
<handlers>
  
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
  
<remove name="OPTIONSVerbHandler" />
  
<remove name="TRACEVerbHandler" />
  
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  
</handlers>
  


  经典模式(classic mode)和集成模式(Integrated mode)比较
  在经典模式下,IIS会用ISAPI扩展(ISAPI extension aspnet_isapi.dll)和 ISAPI过滤器(ISAPI filter aspnet_filter.dll)来调用ASP.NET运行库来处理请求。
  使用经典模式的话,服务器会用两种管道来处理请求一个负责源代码,另外一个负责托管代码。在这种模式下,应用程序不能充分使用IIS7.X提供的服务。
  httpHandler 和 httpModule 在集成和经典模式下的区别
  经典模式,在web.config的 <system.web> 的子节点<httpModules> 加入<add name="MyHttpModule" type="WebApplication.MyHttpModule, WebApplication"/>
  集成模式,在web.config的 <system.webServer> <modules> 加入<add name="MyHttpModule" type="WebApplication.MyHttpModule, WebApplication"/>
页: [1]
查看完整版本: Web.config设置system.webServer