国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

ASP.net的RUL重寫(xiě)

2024-07-10 13:06:59
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

有關(guān)于url的重寫(xiě),本文也只是拿來(lái)主意。相繼有ms的組件“urlrewriter”和在global.asax里的“application_beginrequest()”編碼方式,以及iis里的isapi設(shè)置。

娜列下來(lái),實(shí)現(xiàn)方法也都很簡(jiǎn)單。

 

方法一:ms組件

這里也不用詳解了,相關(guān)請(qǐng)看:

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

用法很簡(jiǎn)單,只需要把組件urlrewriter.dll拷到應(yīng)用程序的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

效果出來(lái)了。


上面的<lookfor>~/(/d{4})/(/d{2})/news/.aspx</lookfor>這句這正則表達(dá)式url,即被重寫(xiě)的url,而<sendto>~/default.aspx?id=$1</sendto>這一句為原始url地址。其中的$1為第一個(gè)正則表達(dá)式值(上面例子為:2004),以此類(lèi)推,第二個(gè)即為$2

 

方法二:application_beginrequest()

在應(yīng)用程序中新建一個(gè)xml文件,文件內(nèi)容為:文件名rewriter.config

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

<rewriterurls>

     <rule>

          <old>(.*)/news/(/d{4})/default/.aspx</old>

          <new>../../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 ("http://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

效果出來(lái)了。

 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 曲松县| 台中县| 嘉荫县| 安陆市| 开阳县| 昌吉市| 东港市| 枣庄市| 安阳县| 镶黄旗| 夹江县| 阜宁县| 延津县| 肥城市| 台南市| 永春县| 屏山县| 宽城| 诸暨市| 南康市| 建瓯市| 中阳县| 安图县| 白沙| 黑水县| 高青县| 永胜县| 开鲁县| 华容县| 景德镇市| 农安县| 呈贡县| 洪洞县| 新兴县| 乌拉特中旗| 凌源市| 武平县| 久治县| 墨脱县| 兴山县| 龙南县|