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

关于HttpModule做伪静态的问题
遇到这么一个问题:域名是http://www.abc.com
然后应用程序放在blog虚拟目录下面,主键查询ID作为一个目录,例如:http://www.abc.com/blog/zhangsan
我想实现的功能就是输入http://www.abc.com/blog/zhangsan就直接跳转到张三的主页
我自己写了一个HttpModule,但没反应,调试中Module完全不起作用,直接返回404
下面是我的代码:
C# code


/// <summary>
        /// Begin Request
        /// </summary>
        /// <param name="sender">HttpApplication</param>
        /// <param name="e">Event Args.</param>
        public void context_BeginRequest(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication)sender;
            String Prefix = "/";

            if (System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath != "/")
            {
                Prefix = System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath + "/";
            }
            Regex r1 = new Regex(Prefix + @"([0-9a-zA-Z\-]*)", RegexOptions.IgnoreCase);
            String Query = app.Request.Url.Query;
            if (Query.StartsWith("?"))
            {
                Query = "&" + Query.Substring(1, Query.Length - 1);
            }

            if (r1.Match(app.Request.Path).Success)
            {
                var agentno = DataAccess.Provider.SzAgentHelper.GetAgentNoByName(r1.Match(app.Request.Path).Groups[1].Value);
                if (agentno != null)
                    app.Context.RewritePath("~/blog/agent.aspx?id=E-" + agentno, false);
            }
}



这个是web.config文件
 <httpModules>
  <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  <add name="SZBlogModule" type="SZBlogModule, WebUI"/>
</httpModules>


难道IIS里需要什么特殊设置吗?
请兄弟们帮忙,谢谢!~··

------解决方案--------------------
http://www.abc.com/blog/zhangsan
没有最后后缀名的的确是要在IIS设置一下

google一下: URL重写 IIS (中间有空格)
应该会找到一些资料
good luck
------解决方案--------------------
C# code

 void Application_BeginRequest(Object sender, EventArgs e)
    {
        String oldUrl = System.Web.HttpContext.Current.Request.RawUrl;

        //其中()最为一个整体,.+任意多个除了()以外的字符,^与字符串开始的地方匹配
        //d+任意多个数字,w+任意多个字母或数字或下划线
        String pattern = @"^(.+)/test4/(d+).aspx/(w+)";
        String replace = @"~/test4.aspx?NID=$1&id=$2&uid=$3";
        if(Regex.IsMatch(oldUrl,pattern,RegexOptions.IgnoreCase | RegexOptions.Compiled))
        {
            String newUrl=Regex.Replace(oldUrl,pattern,replace,RegexOptions.Compiled| RegexOptions.IgnoreCase);
            Context.RewritePath(newUrl);
        }
    }

------解决方案--------------------
伪静态