日期:2014-05-18  浏览次数:21025 次

使用IHttpModule实现URL重写
C# code

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() { }
    }
}




这是我写的URL重写的代码

比如我输入:www.rbmm.cn/renben/index.aspx 需要解析成:www.rbmm.cn/BuildHtml/100006002/index.aspx 而实际没有目录:renben ,系统提示404错误。
我怎么做的没有 renben目录 解析到BuildHtml/100006002目录

------解决方案--------------------
推荐楼主看一下:
http://www.cnblogs.com/luckdv/articles/1687942.html
希望对你有帮助
------解决方案--------------------
没那个路径,估计newUrl,有问题,iis上的网站目录与调试目录是不一样的,看下

另外,你rewrite,用这个有点复杂了,而且,你没用正则,灵活性太差,匹配的逻辑修改,要重编译,麻烦

推荐直接webconfig配置的,直观,灵活