• Url rewriter 原代码


    How URL rewriting accepts a url and rewrites it
    参考文章
    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
  • 相关阅读:
    ReSharper Tips—GotoImplementation
    Possible multiple enumeration of IEnumerable
    Hello, Razor!
    自话自说——POI使用需要注意一个地方
    css中怎么设置透明度的问题
    记录排查国标直播流播放卡顿的问题
    互联网上做广告的优点
    C#、.Net经典面试题集锦(一)
    什么是MFC
    C/S与B/S 的区别
  • 原文地址:https://www.cnblogs.com/flyfish/p/82652.html
Copyright © 2020-2023  润新知