当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > asp.net UrlReWriter使用经验小结

ASP.NET
FreeTextBox(版本3.1.6)在ASP.Net 2.0中使用方法
.NET 常用功能和代码小结
在 .NET Framework 2.0 中未处理的异常导致基于 ASP.NET 的应用程序意外退出
asp.net IList查询数据后格式化数据再绑定控件
asp.net sql存储过程
asp.net 简单实现禁用或启用页面中的某一类型的控件
asp.net(c#)获取内容第一张图片地址的函数
The remote procedure call failed and did not execute的解决办法
ASP.NET 在线文件管理
asp.net 读取并修改config文件实现代码
ASP.NET Cookie 操作实现
asp.net Silverlight中的模式窗体
Silverlight中动态获取Web Service地址
asp.net Silverlight应用程序中获取载体aspx页面参数
asp.net 水晶报表隔行换色实现方法
asp.net 获取Gridview隐藏列的值
手动把asp.net的类生成dll文件的方法
asp.net 使用ObjectDataSource控件在ASP.NET中实现Ajax真分页
动态指定任意类型的ObjectDataSource对象的查询参数
asp.net Md5的用法小结

ASP.NET 中的 asp.net UrlReWriter使用经验小结


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 68 ::
收藏到网摘: n/a

UrlRewriter 是微软封装好了的一个URL重写组件。使用它可以让我节约很多自已开发的时间。 好了,开始讲述我的应用经验,这只是很菜鸟的经验,高手就不用看了。 第一步,请从此下载该示例源码。解压,把UrlRewriter.dll copy到你的项目 bin 目录下。
第二步,在Web.config中加入:
复制代码 代码如下:

<?xml version="1.0" encoding="gb2312" ?>
<configuration>
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>

第二步,加入重写的规则节点:
如:
复制代码 代码如下:

<RewriterConfig>
<Rules>
<RewriterRule>
<LookFor>~/Sell/(.[0-9]*)\.html</LookFor>
<SendTo>~/Search/Search_Sell.aspx?id=$1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Sell/Search_Sell\.aspx</LookFor>
<SendTo>~/Search/Search_Sell.aspx</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Buy/(.[0-9]*)\.html</LookFor>
<SendTo>~/Search/Search_Buy.aspx?id=$1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Buys/(.[0-9]*)\.html</LookFor>
<SendTo>~/Buys/Show.aspx?id=$1</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>

这个就要根据你的需要了,如果你对正则表达式不熟,那么没办法,要么凭借你的高智商去找其中规律,稍稍改一下就能为你所用了。呵呵。如果实在搞不清,那就自己GOOGLE一下正则表达式吧。(本人开始是参考别人的配置猜的,竟然用对了,呵呵。后来还是看了一下相关资料,发现这东东很有用。)
第三步,加入模块配置(写在<system.web>里面):
如:
复制代码 代码如下:

<httpHandlers>
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>

(这里表示使用HTTP程序来处理重写)
好了,到了现在我们可以试一下看。
于是输入:http://127.0.0.1:8080/Sell/1.aspx 出现了,呵呵。但是如果所它改为:http://127.0.0.1:8080/Sell/1.html
晕,发现不行。汗。。。
呵呵,原因是没把HTML的解析用 asp.net 的ISAPI来解析。
办法是。。。
第四步,在IIS\你的站点\属性\主目录\配置\映谢 加入一个和 aspx 页面的配置相同的扩展名项。注意“确认文件是否存在”不要勾选,否则会出现找不到文件。
现在再来试试看。什么?#¥%#¥%#,还是不行。呵呵。不要急,咱们回过头再来看看,原来在 web.config 中我们没有配置 .html 也使用模块此解析。
第五步,在模块配置中加入:
复制代码 代码如下:

<httpHandlers>
<add verb="*" path="*.aspx" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
<add verb="*" path="*.html" type="URLRewriter.RewriterFactoryHandler, URLRewriter" />
</httpHandlers>

现在总可以了吧,呵呵。终于看到了,兴奋吧。不要急,这还只是最简单的。如果你的页面有回传。比如说放了DATAGRID,有分页的,你点到下一页就发现,晕倒,又出问题了。
这下怎么办呢,这个其实微软件的网站上就有说到,我在这里简述一下了。
第六步,加入窗体回传保持的组件:
在原来你下载的项目里找到 ActionlessForm.dll 放到你的项目 bin 目录下。
然后在你的这个页面中加入:
复制代码 代码如下:

<%@ Register TagPrefix="skm" Namespace="ActionlessForm" Assembly="ActionlessForm" %>
再把你的<Form...>改为:
<skm:Form id="你的表单名" method="post" runat="server">
.....
</skm:Form>