这几天关注地址重写问题,参考了微软的
UrlRewriter
。尝试依葫芦画瓢写了自己的一个
UrlRewriting
。
一、参数
1
、
Enabled
:
bool
值,设置是否应用
Url
重写功能;
2
、
IsUseCache
:
bool
,设置是否启用
Cache
功能;(写该功能时,没有测试开启与未开启
Cache
功能对性能带来多少的提高,写该功能只要是练习
Cache
的设置)
3
、
UrlRules
:
string
类型,设置
Url
重写规则,设置时字符串开头必须包含
"~"
。例:
UrlRules=
“
~/Article/(/d+)/show.html
”;
4
、
OrignalUrl
:
string
类型,设置
Url
重写的原始地址,设置时字符串开头必须包含
"~"
,且相对于网站主目录的
Url
地址必须正确。例:
OrignalUrl='~/articleshow.aspx?aid=$1'
;
二、
Web.Config
配置
1
、
configSections
配置
<configSections>
<section name="UrlRewritings" type="Nimeux.UrlRewriting.UrlRewritingSection,Nimeux.UrlRewriting"/>
</configSections>
<UrlRewritings Enabled="false" IsUseCache="false">
<add UrlRules="~/(/d+)/(/d+)/show/.info" OrignalUrl="~/default.aspx?year=$1&month=$2"/>
<add UrlRules="~/show_(/d+)/.info" OrignalUrl="~/showarticle.aspx?aid=$1"></add>
</UrlRewritings>
2
、
httpModules
、
httpHandlers
设置
<httpModules>
<add name="UrlRewritingModule" type="Nimeux.UrlRewriting.UrlRewritingModule,Nimeux.UrlRewriting"/>
</httpModules>
<httpHandlers>
<add verb="*" path="*.info" type="Nimeux.UrlRewriting.UrlRewritingHeadler,Nimeux.UrlRewriting"/>
</httpHandlers>
如果站点设置时不更改
*.aspx
后缀名,那么就不用配置
<httpHanders>
三、这面连接地址
这个
UrlRewriting
有一个让人很难受的问题,那就是页面地址设置的比灵活。例如:页面
Default.aspx
中有一个连接
<a href="2007/10/show.info">test</a>
,他所指向的页面为
"Default.aspx?year=2007&month=10",
当页面加载后,第一次点击该连接,则成功连接且
Url
重写成功。但当在一次点击页面时,链接的地址栏就变成
http
:
//localhost/2007/10/2007/10/show.info
,显然这个结果是错误的。
解决这个问题,我尝试的一些方法:
(1)
使用
asp.net
服务器控件时,设置
NavigateUrl="~/2007/10/show.info",
这样就解决了上述问题
(2)
也是用
<a>
标记,把
href
设置为
href="http://localhost/2007/10/show.info"
,但是这样
VS
自带的默认
Web
服务器下调时就要加入主目录文件夹名(本例中为
/WebTest/
),也就是链接地址为
href="http://localhost:4325/WebTest/2007/10/show.info
”,在这样带来了不便。在此把这些问题提出,希望各位大侠帮我解决这个问题,这里表示感谢。
四、页面设置好后,就进行
IIS
配置,如果采用
IIS
已经默认的后缀名,则没有必要配置
IIS
了。但如果更改了页面的后缀名,如本例中的
*.info,
则要在
IIS
默认网站中选择属性
/
主目录
/
配置
/
添加新的文件后缀名。
好了,以上就是这两天的所学所得,写出来供大家批评指正。源代码下载地址:(http://download.csdn.net/source/272595)