asp.net 实现URL重写方法如下:
一,获得Mircosoft URLRewriter.dll: 获得Mircosoft URLRewriter.dll可以到http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx?mfr=true
下载完毕后,导入工程,我这里没有对该工程做任何修改,保留了原来的重写方式,然后直接在VS2005里面生成.dll文件就可以了。 二,使用该dll文件: 添加引用,搞定。 三,页面方面的设计,这里不在赘述了,我会放一个下载包,有兴趣的朋友下载来看看吧,代码写的比较乱。 四,web.config的配置 这部是非常关键的,也是静态化能否成功的关键。
view plaincopy to clipboardprint? 01.<?xml version="1.0"?> 02.<configuration> 03. <configSections> 04. <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" /> 05. </configSections> 06. 07. <RewriterConfig> 08. <Rules> 09. <RewriterRule> 10. <LookFor>~/web/new/type/(.[0-9]*)\.html</LookFor> 11. <SendTo>~/web/new.aspx?id=$1</SendTo> 12. </RewriterRule> 13. <RewriterRule> 14. <LookFor>~/web/index.html</LookFor> 15. <SendTo>~/web/index.aspx</SendTo> 16. </RewriterRule> 17. </Rules> 18. </RewriterConfig> 19. <system.web> 20. <httpHandlers> 21. <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" /> 22. <add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" /> 23. </httpHandlers> 24. <compilation debug="true"/></system.web> 25.</configuration> <?xml version="1.0"?> <configuration> <configSections> <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" /> </configSections>
<RewriterConfig> <Rules> <RewriterRule> <LookFor>~/web/new/type/(.[0-9]*)\.html</LookFor> <SendTo>~/web/new.aspx?id=$1</SendTo> </RewriterRule> <RewriterRule> <LookFor>~/web/index.html</LookFor> <SendTo>~/web/index.aspx</SendTo> </RewriterRule> </Rules> </RewriterConfig> <system.web> <httpHandlers> <add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" /> <add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" /> </httpHandlers> <compilation debug="true"/></system.web> </configuration>
这里简单介绍一下:
view plaincopy to clipboardprint? 01.<RewriterConfig> 02. <Rules> 03. <RewriterRule> 04. <LookFor>要查找的模式</LookFor> 05. <SendTo>要用来替换模式的字符串</SendTo> 06. </RewriterRule> 07. <RewriterRule> 08. <LookFor>要查找的模式</LookFor> 09. <SendTo>要用来替换模式的字符串</SendTo> 10. </RewriterRule> 11. </Rules> 12.</RewriterConfig> <RewriterConfig> <Rules> <RewriterRule> <LookFor>要查找的模式</LookFor> <SendTo>要用来替换模式的字符串</SendTo> </RewriterRule> <RewriterRule> <LookFor>要查找的模式</LookFor> <SendTo>要用来替换模式的字符串</SendTo> </RewriterRule> </Rules> </RewriterConfig>
httpHandlers的设置主要是配合IIS将请求重新定义处理,这里也比较关键,如果不存在合理的httpHandlers,那么,访问肯定会失败的。
关于正则表达式,可以到百度里搜索:"常用正则表达式",会有很多。
五.配置IIS解析.html文件 右键点我的电脑-->管理-->展开'服务和应用程序'-->internet信息服务-->找到你共享的目录-->右键点击属性 -->点击'配置'-->映射下面 -->找到.aspx的可执行文件路径 复制路径-->粘贴路径-->扩展名为".html"-->然后把检查文件是否存在的勾去掉这样就可以了,如果遇到“确定”按钮失效,可以用键盘事件编辑路径即可解决。
(责任编辑:admin) |