代码改变世界

IIS7下使用URLRewriter的配置

2011-04-30 10:18  jaywoo  阅读(1416)  评论(1编辑  收藏  举报

引用Lixy的博文使用Microsoft URL Rewrite Module for IIS 7.0修改WEB.CONFIG即可实现*.HTML伪静态 无需修改应用程序映射 :在IIS5和IIS6时代,我们使用URL REWRITING可实现URL重写,使得WEB程序实现伪静态,但默认情况下只能实现.ASPX的伪静态,如果要实现伪静态*.HTML的页面,需要将ISAPI里面的*.HTML应用程序映射改为.NET的ISAPI。但在IIS 7时代,这一切已经变得非常简单了,您在WEB.CONFIG中就可以管理这一切了。

可我今天改的程序是我以前写的,在II6中配置实现的伪静态,现在在IIS7中跑也不想去重配置伪静态规则了,所就想在IIS7中实现URLRewriter的配置。

   一:和IIS6一样 在<configSections>节点下配置

<configuration>
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>
<RewriterConfig> 
<Rules>     
<RewriterRule>        
<LookFor>~/about/feedback\.html</LookFor>       
<SendTo><![CDATA[~/About/FeedBack.aspx]]></SendTo>     
</RewriterRule>
</Rules>
</RewriterConfig>
<httpModules>
<add name="ModuleRewriter" type="URLRewriter.ModuleRewriter, URLRewriter" />
</httpModules>
二 :在IIS7中将ISAPI里面的*.HTML应用程序映射改为.NET的ISAPI
在“处理程序映射”中添加脚本映射 配置如下
点击确定的时候会自动在Web.Config中添加
<handlers>
<remove name="WebServiceHandlerFactory-Integrated" />
<remove name="ScriptHandlerFactory" />
<remove name="ScriptHandlerFactoryAppServices" />
<remove name="ScriptResource" />
<add name="HtmlHandler" path="*.html" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll"
 resourceType="Unspecified" preCondition="classicMode,runtimeVersionv2.0,bitness32" />
</handlers>
 三:如我的站点为:http://192.168.1.3/  则http://192.168.1.3/about/feedback.html 是可以正常打开的也就说我们伪静态配置好了,
但可当你打开原本就是.html文件 (如我站点下的stiemap.html) 当我打开http://192.168.1.3/sitemap.html 时,问题出现了:
        

“/”应用程序中的服务器错误。

没有为扩展名“.html”注册的生成提供程序。可以在 machine.config 或 web.config 中的

<compilation><buildProviders> 节注册一个。请确保所注册的提供程序具有包含值“Web”或“All”的

BuildProviderAppliesToAttribute 属性。

这时要做如下配置:

<compilation debug="true">
<!-- 加上此节点,保证原本就是.html类型的文件能正常访问 -->
<buildProviders>
<add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
</buildProviders>
</compilation>