Asp.net Url改写方法
时间:2011-01-12 来源:宽田
一、伪静态方式改写方法
下载地址2:download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi
此方案中有三个项目:
URLRewriter:伪静态URL使用。真正实现重写的是此项目。
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>
<Rules>
<!--
Rules for Blog Content Displayer
为了便于观察结果。我把规则<LookFor>中的appx给删除了
-->
<RewriterRule>
<LookFor>~/(\d{4})/(\d{2})/(\d{2})</LookFor>
<SendTo>~/ShowBlogContent.aspx?year=$1&month=$2&day=$3</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/(\d{4})/(\d{2})/Default</LookFor>
<SendTo><![CDATA[~/ShowBlogContent.aspx?year=$1&month=$2]]></SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/(\d{4})/Default</LookFor>
<SendTo>~/ShowBlogContent.aspx?year=$1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Default</LookFor>
<SendTo>~/Default.aspx</SendTo>
</RewriterRule>
<!-- Rules for Product Lister -->
<RewriterRule>
<LookFor>~/Products/Default</LookFor>
<SendTo>~/ListCategories.aspx</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Beverages</LookFor>
<SendTo>~/ListProductsByCategoryNo.aspx?CategoryID=1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Condiments</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=2</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Confections</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=3</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Dairy</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=4</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/GrainsCereals</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=5</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/MeatPoultry</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=6</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Produce</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=7</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Seafood</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=8</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
</httpModules>
用于指定配置节"RewriterConfig"的处理程序类的名称为”URLRewriter.Config.RewriterConfigSerializerSectionHandler”,该类存在于bin目录下的URLRewriter.dll文件中
(2)、<RewriterRule/>标签里面就是重写的一个模块, <LookFor/>里面的是重写后的地址。注LookFor中有的使用了正则来匹配相应的页面。如: <LookFor>~/(\d{4})/Default</LookFor>
<SendTo/>则是原地址。 如果原地址中用到参数,可以用“$索引号”的方式来调用lookFor中相应的值,
运行项目,以地址栏中输入 http://hostname/2003/Default,
服务器会指向http://hostname/ShowBlogContent.aspx?year=2003的页面。
1、UrlRewriter说明
UrlRewriter 是微软封装好了的一个URL重写组件。
下载地址2:download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi
此方案中有三个项目:
URLRewriter:伪静态URL使用。真正实现重写的是此项目。
ActionlessForm: 处理本页面的回发使用,比如分页。
RewriterTester:为测试项目 要实现改写只需要在项目中引用URLRewriter.dll。
2、使用方法
UrlRewriter使用比较简单,只需要在Web.Config中配置一下即可。下边我用微软件提供的例子RewriterTester作说明。
1)在需要改写的URL的项目中引用生成的URLRewriter.dll。
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>
<RewriterConfig>
<Rules>
<!--
Rules for Blog Content Displayer
为了便于观察结果。我把规则<LookFor>中的appx给删除了
-->
<RewriterRule>
<LookFor>~/(\d{4})/(\d{2})/(\d{2})</LookFor>
<SendTo>~/ShowBlogContent.aspx?year=$1&month=$2&day=$3</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/(\d{4})/(\d{2})/Default</LookFor>
<SendTo><![CDATA[~/ShowBlogContent.aspx?year=$1&month=$2]]></SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/(\d{4})/Default</LookFor>
<SendTo>~/ShowBlogContent.aspx?year=$1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Default</LookFor>
<SendTo>~/Default.aspx</SendTo>
</RewriterRule>
<!-- Rules for Product Lister -->
<RewriterRule>
<LookFor>~/Products/Default</LookFor>
<SendTo>~/ListCategories.aspx</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Beverages</LookFor>
<SendTo>~/ListProductsByCategoryNo.aspx?CategoryID=1</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Condiments</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=2</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Confections</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=3</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Dairy</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=4</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/GrainsCereals</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=5</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/MeatPoultry</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=6</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Produce</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=7</SendTo>
</RewriterRule>
<RewriterRule>
<LookFor>~/Products/Seafood</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=8</SendTo>
</RewriterRule>
</Rules>
</RewriterConfig>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
</httpModules>
配置说明:
用于指定配置节"RewriterConfig"的处理程序类的名称为”URLRewriter.Config.RewriterConfigSerializerSectionHandler”,该类存在于bin目录下的URLRewriter.dll文件中
(2)、<RewriterRule/>标签里面就是重写的一个模块, <LookFor/>里面的是重写后的地址。注LookFor中有的使用了正则来匹配相应的页面。如: <LookFor>~/(\d{4})/Default</LookFor>
<SendTo/>则是原地址。 如果原地址中用到参数,可以用“$索引号”的方式来调用lookFor中相应的值,
如<SendTo>~/ShowBlogContent.aspx?year=$1</SendTo>。$1表示调用<LookFor>中的第一个正则表达式。
3)测试结果运行项目,以地址栏中输入 http://hostname/2003/Default,
服务器会指向http://hostname/ShowBlogContent.aspx?year=2003的页面。
相关阅读 更多 +
排行榜 更多 +