Urlrewriter

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>

<RewriterConfig> <Rules>     
<RewriterRule> <LookFor>~/(\d{4})/(\d{2})/(\d{2})\.aspx</LookFor> <SendTo>~/ShowBlogContent.aspx?year=$1&amp;month=$2&amp;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> <RewriterRule> <LookFor>~/ModalPopupExtender</LookFor> <SendTo>~/ModalPopupExtender.aspx</SendTo> </RewriterRule> </Rules> </RewriterConfig>

参考资料 http://www.cnblogs.com/nyzfl/articles/825184.html

posted @ 2012-07-03 09:13  好运博客  阅读(127)  评论(0)    收藏  举报