C#技术百科
问问你的心你有没有信心 做事情要专一坚定,执着

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace UI
{
    /// <summary>
    /// URL地址重写,Config配置读取
    /// </summary>
    public sealed class CustomSectionsHandler
    {
        public static XmlNode GetSection(string sectionName, string configFilePath)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(GetCustomSection(sectionName, configFilePath));
            return doc;
        }

        private static string GetCustomSection(string sectionName, string configFilePath)
        {
            string section = string.Empty;
            XmlReaderSettings xrs = new XmlReaderSettings();
            xrs.IgnoreComments = true;
            xrs.IgnoreProcessingInstructions = true;
            xrs.IgnoreWhitespace = true;
            XmlReader xr = XmlReader.Create(configFilePath, xrs);
            while (xr.Read())
            {
                if (xr.NodeType == XmlNodeType.Element)
                {
                    if (xr.Name == sectionName)
                    {
                        section = xr.ReadOuterXml();
                        xr.Close();
                        return section;
                    }
                }
            }
            xr.Close();
            return section;
        }
    }
}

 

 

////handerl

 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Web.Caching;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
namespace .UI
{
    /// <summary>
    /// URL地址重写
    /// </summary>
    public class UrlRewriteHandler : IHttpHandlerFactory
    {
        public IHttpHandler GetHandler(HttpContext context, string requestType, string url, string physicalPath)
        {
           // Response.Write("ddd");
            string sendUrl = url, sendFilePath = physicalPath;
          
            if (context.Cache["RewriteRules"] == null)
            {
                context.Cache.Insert("RewriteRules", GetPatternRules());
            }
            ArrayList al = (ArrayList)context.Cache["RewriteRules"];
            //ArrayList al = GetPatternRules();
            for (int i = 0; i < al.Count; i++)
            {
                string[] rule = al[i].ToString().Split("~".ToCharArray());
                Regex rg = new Regex(rule[0], RegexOptions.IgnoreCase);
               // context.Response.Write("sendUrl--" + sendUrl + "--sendFilePath---" + sendFilePath + "---url--" + url + "--rule[1]--" + rule[1] + "<br>");
                if (rg.IsMatch(sendUrl))
                {
                    url = rg.Replace(url, rule[1]);
                    UrlRewrite(context, url, out sendUrl, out sendFilePath);
                    return PageParser.GetCompiledPageInstance(sendUrl, sendFilePath, context);
                }
            }
            return PageParser.GetCompiledPageInstance(sendUrl, sendFilePath, context);
        }

        private void UrlRewrite(HttpContext context, string url, out string sendUrl, out string sendFilePath)
        {
            string queryString = string.Empty;
            sendUrl = url;
            if (url.IndexOf("?") > 0)
            {
                sendUrl = url.Substring(0, url.IndexOf("?"));
                queryString = url.Substring(url.IndexOf("?") + 1);
            }
            sendFilePath = context.Server.MapPath(sendUrl);
            context.RewritePath(sendUrl, string.Empty, queryString);
        }
        private ArrayList GetPatternRules()
        {
            ArrayList al = new ArrayList();
            XmlNode xn = CustomSectionsHandler.GetSection("UrlRewriteSection", HttpContext.Current.Server.MapPath("~/web.config"));
            foreach (XmlNode x in xn.FirstChild.ChildNodes)
            {
                string rule = string.Empty;
                foreach (XmlNode n in x.ChildNodes)
                {
                    rule += n.InnerText;
                }
                al.Add(rule);
            }
            return al;
        }
        public void ReleaseHandler(IHttpHandler hadler)
        {
        }
    }

}

 

webconfig

配置

<configSections>
    <section name="GnhaoCustomSection" type="UI.UrlRewriteHandler"/>

<configSections>
  <GnhaoCustomSection>

    <UrlRewriteSection>

  <Rule>
        <LookFor><![CDATA[http://(\w+)\.\.com]]></LookFor>
        <SendTo><![CDATA[~/index.aspx?id=$1]]></SendTo>
      </Rule>
   
      <!--html end-->
    </UrlRewriteSection>
  </GnhaoCustomSection>

 

    <add verb="POST,GET" path="*.htm" type=".UI.UrlRewriteHandler"/>
      </httpHandlers>

posted on 2008-08-22 16:39  王德田  阅读(231)  评论(0编辑  收藏  举报