Url rewriter 原代碼

來源:互聯網
上載者:User
  • Download source - 8 Kb


參考文章

http://www.cnblogs.com/edobnet/archive/2004/06/02/12866.aspx

參考代碼

Global.aspx.cs:



        protected void 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

            ...{

            }


        }


ReWriter.config ,配製檔案

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

<ReWriterUrls>

        <rule>

            <old>(.*)/TestUrlRe/file(.*)/(.*).html</old>

            <new>../WebForm1.aspx?id=$2&amp;type=$3</new>

        </rule>

        <rule>

            <old>(.*)/TestUrlRe/t(.*)/(.*).html</old>

            <new>../WebForm1.aspx?tid=$2&amp;ttype=$3</new>

        </rule>

</ReWriterUrls> 

http://www.cnblogs.com/edobne

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.