日期:2014-05-18 浏览次数:21294 次
using System;
using System.Web;
using System.Collections;
namespace HttpURLModule
{
    public class HttpReWriter : IHttpModule
    {
        public void Init(HttpApplication context)
        {
            context.BeginRequest += new EventHandler(contextRequest);
        }
        private void contextRequest(object sender, EventArgs e)
        {
            /*
            www.rbmm.cn/rbxx/Index.html创建单位主页
            www.rbmm.cn/rbxx/zzld_Index.html?id=1创建单位栏位主页
            www.rbmm.cn/rbxx/zzld_201201_2.html内容
            BuildHtml/100006002/Index.html
            */
            HttpApplication app = sender as HttpApplication;
            Hashtable DktUrl = app.Application["DKTUrl"] as Hashtable;
            if (app == null) return;
            string currentUrl = app.Context.Request.RawUrl.TrimEnd('/');
            string[] slUrl = currentUrl.Split('/');
            string newUrl = currentUrl;
            if (slUrl.Length == 2)
            {
                object objValue = DktUrl[slUrl[1].ToLower()];
                if (objValue != null)
                {
                    newUrl = slUrl[0] + "/BuildHtml/" + (string)objValue + "/Index.html";
                }
            }
            else if (slUrl.Length > 2)
            {
                object objValue = DktUrl[slUrl[1].ToLower()];
                if (objValue != null)
                {
                    newUrl = currentUrl.Replace("/" + slUrl[1] + "/", "/BuildHtml/" + (string)objValue + "/");
                }
            }
            app.Context.RewritePath(newUrl);
        }
        public void Dispose() { }
    }
}