• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

ASP.net的RUL重写

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

有关于URL的重写,本文也只是拿来主意。相继有MS的组件“URLRewriter”和在Global.asax里的“Application_BeginRequest()”编码方式,以及IIS里的ISAPI设置。

娜列下来,实现方法也都很简单。

 

方法一:MS组件

这里也不用详解了,相关请看:

http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx

用法很简单,只需要把组件URLRewriter.dll拷到应用程序的bin目录下,然后在web.config下加入如下代码:

<configuration></configuration>中加入:

     <configSections>

          <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />

     </configSections>

    

     <RewriterConfig>

          <Rules>

              <RewriterRule>

                   <LookFor>~/(\d{4})/(\d{2})/Default\.aspx</LookFor>

                   <SendTo>~/Default.aspx?ID=$1</SendTo>

              </RewriterRule>

          </Rules>

     </RewriterConfig>

 

然后在<system.web></system.web>中加入:

<httpHandlers>

   <add verb="*" path="*.aspx" 

        type="URLRewriter.RewriterFactoryHandler, URLRewriter" />

</httpHandlers>

 

最后在地址栏上键入:http://localhost/Test/2004/12/News.aspx

效果出来了。

上面的<LookFor>~/(\d{4})/(\d{2})/News\.aspx</LookFor>这句这正则表达式URL,即被重写的URL,而<SendTo>~/Default.aspx?ID=$1</SendTo>这一句为原始URL地址。其中的$1为第一个正则表达式值(上面例子为:2004),以此类推,第二个即为$2 

 

方法二:Application_BeginRequest()

在应用程序中新建一个XML文件,文件内容为:文件名ReWriter.config

<?xml version="1.0" encoding="utf-8" ?> 

<ReWriterUrls>

     <rule>

          <old>(.*)/News/(\d{4})/Default\.aspx</old>

          <new>http://www.cnblogs.com/Default.aspx?id=$2&amp;type=$3</new>

     </rule>

</ReWriterUrls>

Global.asax文件中的Application_BeginRequest(Object sender, EventArgs e)加入代码:

              try

              {

                   string path=Server.MapPath("~/ReWriter.config");

                   XPathDocument myXPathDocument = new XPathDocument(path);

                   XPathNavigator myXPathNavigator = myXPathDocument.CreateNavigator();

                   XPathNodeIterator myXPathNodeIterator =  myXPathNavigator.Select ("//rule");

                   System.Text.RegularExpressions.Regex oReg;

                   string ReWriteUrl;

                   while (myXPathNodeIterator.MoveNext())

                   {

                        //oReg=new Regex(oNode.SelectSingleNode("url/text()").Value);

                        XPathNavigator nav2 = myXPathNodeIterator.Current.Clone();

                       string oldString="",newString="";

                        XPathNodeIterator it2 = nav2.Select("old");

                        while(it2.MoveNext())

                       {

                            oldString = it2.Current.Value;

                            break

                       }

                       it2 = nav2.Select("new");

                        while(it2.MoveNext())

                       {

                            newString = it2.Current.Value;

                            break

                       }

                        if(oldString != "" &&  newString != "")

                       {

                            oReg = new System.Text.RegularExpressions.Regex(oldString);

                            if(oReg.IsMatch(Request.Url.ToString()))

                            {

                                 ReWriteUrl = oReg.Replace(Request.Url.ToString(),newString);

                                 HttpContext.Current.RewritePath(ReWriteUrl);

                                 break

                            }

                       }

                   }

              }

              catch

              {

              }

 

最后在地址栏上键入:http://localhost/Test/News/2004/Default.aspx

效果出来了。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
ASP.NETMVCHTMLHELPER类的方法总结发布时间:2022-07-10
下一篇:
ASP.NET MVC 入门1、简介发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap