1.网站Bin目录下添加"ActionlessForm.dll"和"UrlRewriter.dll"两个文件
2.在Web.config配置文件中添加4个节点
<configuration>
<configSections>
<section name="CustomConfiguration" type="URLRewriter.Config.UrlsSection, URLRewriter"/>
</configSections>
</configuration>
<system.web>
<httpModules><
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
</httpModules>
</system.web>
<httpHandlers>
<add path="*.html" verb="*" type="System.Web.UI.PageHandlerFactory" validate="true"/>
</httpHandlers>
<system.web>
<compilation debug="false">
<buildProviders>
<add extension=".html" type="System.Web.Compilation.PageBuildProvider" />
</buildProviders>
</compilation>
</system.web>
3.重写规则,<CustomConfiguration>节点与<appSettings>节点同级,如:.
<RewriterConfig>
<Rules>
<!-- Rules for Blog Content Displayer -->
<RewriterRule>
<LookFor>~/(\d{4})/(\d{2})/(\d{2})\.aspx</LookFor>
<SendTo>~/ShowBlogContent.aspx?year=$1&month=$2&day=$3</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/(\d{4})/(\d{2})/Default\.aspx</LookFor>
<SendTo><![CDATA[~/ShowBlogContent.aspx?year=$1&month=$2]]></SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/(\d{4})/Default\.aspx</LookFor>
<SendTo>~/ShowBlogContent.aspx?year=$1</SendTo>
</RewriterRule>
<!-- Rules for Product Lister -->
<RewriterRule>
<LookFor>~/Products/Default\.aspx</LookFor>
<SendTo>~/ListCategories.aspx</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Beverages\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Condiments\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=2</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Confections\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=3</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Dairy\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=4</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/GrainsCereals\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=5</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/MeatPoultry\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=6</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Produce\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=7</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Seafood\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=8</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
然后,你的 aspx 程序就会按照你 web.config 文件中的 正则表达式,转换url 请求地址,实现 urlrewrit 技术。
比如:
1、http://localhost:4789/GuanTestURLRewrit/2003/07/18.aspx
按照 web.config 文件中的正则,此 url 地址为被 重写到以下真实存在的地址
http://localhost:4789/GuanTestURLRewrit/ShowBlogContent.aspx?year=2003&month=7&day=18
2、http://localhost:4789/GuanTestURLRewrit/2003/default.aspx
按照 web.config 文件中的正则,此 url 地址为被 重写到以下真实存在的地址
http://localhost:4789/GuanTestURLRewrit/ShowBlogContent.aspx?year=2003
3、http://localhost:4789/GuanTestURLRewrit/Products/Confections.aspx
按照 web.config 文件中的正则,此 url 地址为被 重写到以下真实存在的地址
http://localhost:4789/GuanTestURLRewrit/ListProductsByCategory.aspx?CategoryID=3
可以自己定义自己的正则表达式实现不同的 url 重写规则
如果您想把 aspx 重写成 html 后辍名(伪静态),那么则需要改动一下你的 web.config 文件,
<httpHandlers>
<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>
这样好像还不行,那是因为在IIS里面无法解析.html后缀名(具体我也不知道怎么叫...)
然后这样操作:
右键点我的电脑-->管理-->展开'服务和应用程序'-->internet信息服务-->找到你共享的目录-->右键点击属性 -->点击'配置'-->
映射下面 -->找到.aspx的可执行文件路径 复制路径-->粘贴路径-->扩展名为".html"-->然后把检查文件是否存在的勾去掉 这样就可以了
引用 ActionlessForm.dll 文件,是因为当页面中有Post数据(如Post文本)。那么这时重写后的URL就会变为:http://localhost/Test/2004/12/News.aspx?ID=12 真实的 url,露出原始的地址了,这显然是不完善的,
附:为什么URL就会变为:http://localhost/Test/2004/12/News.aspx?ID=12
其实很简单,因为在web.config中有这样的一句:
<SendTo>~/Default.aspx?ID=$2</SendTo>
在没有替换form之前,你查看页面的源码就可以看到,你的form的Action是到(以上面的例子):Default.aspx?ID=12
即.aspx页面最后生成的HTML是:
<form id="Form1" name="Form1" method="post" action="Default.aspx?ID=12"></form>
解决方述问题方法:
首先把ActionlessForm.dll拷入你的项目中的bin目录,然后在你的VS.net的项目中引用这个dll。再在你原有的(即没重写的).aspx页面中
第一步:把这句加于代码顶部:
<%@ Register TagPrefix="skm" Namespace="ActionlessForm" Assembly="ActionlessForm" %>
第二步:
<form id="Form1" method="post" runat="server">和</form>
替换成:
<skm:Form id="Form1" method="post" runat="server">和</skm:Form>
这样,当此页面有回发数据时,则不会跳到真实的 url 上去。
还有,如果想用URL重写后的格式为以目录形式即不用加Default.aspx:
http://localhost/Test/2004/12
则要新建相应的目录和文件Default.aspx。
如上例子:http://localhost/Test/2004/12
则要新建2004目录和在此目录下新建12目录,再在12目录下新建Default.aspx文件。文件内容可为空。
至于为什么,是因为IIS如没有找到目录或文件时会报错。
另外需要注意的是,要在项目中添加 2003,2004 还有月份 01,02.03 .....12 这样的目录,然后在第一个目录下添加 default.aspx ,内容只为一句话:<%@ Page %> 即可,防止用户输入目录回车后,找不到文件,出错。
附上DBank网盘控件下载:http://dl.dbank.com/c0nl717ywv