,添加URLWriter.dll引用到bin目录下
2,在web.config里的<configSections>加入如下部分
<configSections>
<!--urlrewritter-->
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
<!--end-->
3,在<system.web>中的<httpHandles>节点里加入如下配置
<httpHandlers>
<!--添加模块配置-->
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
4,配置重写规则
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/index.html</LookFor>
<SendTo>~/default.aspx</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/news/details_(/d+)_(/d+).html</LookFor>
<SendTo>~/news/details.aspx?tid=$1&nid=$2</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/news/index.html</LookFor>
<SendTo>~/news/default.aspx</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/news/list_(/d+).html</LookFor>
<SendTo>~/news/list.aspx?tid=$1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/(.*).html</LookFor>
<SendTo>~/$1.aspx</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
5,修改IIS配置
重写后的页面在程序里运行没问题,比如在vs里运行后讲地址栏的url改掉
http://localhost:4814/atbouus.aspx手动改为http://localhost:4814/atbouus.html
没问题,可以运行,但是在IIS里配置虚拟目录后,再运行改掉路径
http://localhost/nextuntil/aboutus.aspx改为http://localhost/nextuntil/aboutus.html
发现出错勒,这是因为aboutus.html页面根本不存在,所以需要对IIS配置稍作修改
虚拟目录右键>>>属性>>>(选项卡第一个|虚拟目录>共享位置>配置)>>>映射
点击添加效果如下图:
做些简单的说明勒
<a>,关于重写规则
<LookFor>里面为配置后的地址,<SendTo>才是真正访问的地址
1.上面的例子中给出了一些比较常见的重写规则,比如:default.aspx->index.html
一般用在首页的重写
2.如果很多页面都是.aspx并且没有传参可以通用一个重写规则,上面例子配置规则里最后一个
但是它必须放在其他规则后面,这个可以根据运行顺序理解
3.涉及到参数传递的,1个参数或者多个参数的,上面也有,参数间用$amp;隔开
主要其实就是正则的使用勒(我正则也不太好,所以都很简单的规则做例子勒)
<b>,<httpHandles>里配置说明
这里其实只有2句,需要说明的是,这里的例子配置的重写规则都是.html的后缀
而如果项目里有些页面原本的后缀就是.html的话
会因为配置规则的最后一句造成"干扰"
譬如有一页面名为sitemap.html原本可以直接访问
但是由于重写规则的存在,会导致去访问sitemap.aspx,这个页面自然是不存在的
所以我们需要加上对html的模块配置,就是这里的第二句,注意顺序,道理其实同配置规则里 2 的一样
<c>,其他
其他还有蛮多说法,比如
1,重写为目录格式像 article/ajax/ 这种
2,或者实现任意二级域名 等等,后续勒、
3,重写规则比较多的时候,可以单独建一个xml文件,web.config里配置下指定
具体操作如下:
</configSections>
<!--加入url重写规则定义的引用-->
<RewriterConfig configSource="WebReURL.config" />
<appSettings/>
然后自己建一个WebReURL.config配置文件
专门用来写入重写规则,格式如下:
<?xml version="1.0" encoding="utf-8"?>
<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/news/index.html</LookFor>
<SendTo>~/news/default.aspx</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/2012/index.html</LookFor>
<SendTo>~/2012/default.aspx</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
大致就写这么多勒