asp.net的伪静态技术

首先在以下地址:
http://download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi

下载 MS 的 URLRewriter.dll,放到你的web程序的bin下。

注:以上地址下载的是微软的一个完整的 URLrewrite 技术示例。下载后是一个 MSDNURLRewriting.msi 文件,安装在本地机上,安装后,在安装目录内有三个文件夹,分别是:ActionlessForm ,RewriterTester,URLRewriter 这三个目录。 其中 URLRewriter 文件夹便是一个完整的 URLRewrite 的项目示例。此项目中的 BIN 目录中有两 个 dll,分别为
ActionlessForm.dllURLRewriter.dll ,这两个 dll 就是项目 ActionlessFormURLRewriter 产生的 dll 类库,是示例项目 RewriterTester 实现 URLRwrite 技术所用到的类库文件。

如何把此技术应用到你自己的项目中去,其实很简单:

首先,把 ActionlessForm.dllURLRewriter.dll 两个 dll 文件放到你自己项目中的 bin 目录下。
然后,修改你的 web.config 文件,完整的 web.config 文件如下:

(只需在普通的 web.config 文件中填加两个地方)
----------------------------------------------
1、

</configSections> 标签上面填加:
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />

2、

<httpModules>
      <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
   </httpModules>


--------------------------------------------

然后,你的 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="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
     <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 上去。
 
posted @ 2010-03-03 17:18  why520crazy  阅读(227)  评论(1编辑  收藏  举报