Asp.Net Url改写方法
一、伪静态方式(使用URLRewriter)1、改写方法 (文章地址:http://www.cnblogs.com/scottckt/archive/2011/01/12/1933737.html)
2、回发时处理方法(文章地址:http://www.cnblogs.com/scottckt/archive/2011/01/12/1933791.html)
3、将Aspx改写成HTml方法(文章地址:http://www.cnblogs.com/scottckt/archive/2011/01/12/1933836.html)
二、真实改写(使用System.Web.Routing)(文章地址:http://www.cnblogs.com/scottckt/archive/2011/01/12/1933893.html)
一、伪静态方式改写方法
1、UrlRewriter说明
UrlRewriter 是微软封装好了的一个URL重写组件。
下载地址:download.microsoft.com/download/0/4/6/0463611e-a3f9-490d-a08c-877a83b797cf/MSDNURLRewriting.msi
此方案中有三个项目:
下载地址: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。
2)配置WEB.CONFIG文件,以Web.Config中添加经下内容。
<configSections>
<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
</configSections>
<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>
<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>
<httpModules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
</httpModules>
<add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
</httpModules>
配置说明:
(1)、<section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
用于指定配置节"RewriterConfig"的处理程序类的名称为”URLRewriter.Config.RewriterConfigSerializerSectionHandler”,该类存在于bin目录下的URLRewriter.dll文件中
<SendTo/>则是原地址。 如果原地址中用到参数,可以用“$索引号”的方式来调用lookFor中相应的值,
用于指定配置节"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的页面。
服务器会指向http://hostname/ShowBlogContent.aspx?year=2003的页面。
用此方法注意:
1.不能使用Windows身份验证用户权限. 应使用Form验证,在web.config配置为:<authentication mode="Forms" />
2.使用Request.ServerVariables["script_name"]获得的路径仍然是:ShowPlay.asp?vid=1
3.被重写的地址如果回发,重写将失效,显示的地址将是ShowPlay.asp?vid=1
1.不能使用Windows身份验证用户权限. 应使用Form验证,在web.config配置为:<authentication mode="Forms" />
2.使用Request.ServerVariables["script_name"]获得的路径仍然是:ShowPlay.asp?vid=1
3.被重写的地址如果回发,重写将失效,显示的地址将是ShowPlay.asp?vid=1
参考文章:http://www.cnblogs.com/rickel/archive/2007/02/04/639616.html