http://www.sufeinet.com/plugin.php?id=keke_group

苏飞论坛

 找回密码
 马上注册

QQ登录

只需一步,快速开始

分布式系统框架(V2.0) 轻松承载百亿数据,千万流量!讨论专区 - 源码下载 - 官方教程

HttpHelper爬虫框架(V2.7-含.netcore) HttpHelper官方出品,爬虫框架讨论区 - 源码下载 - 在线测试和代码生成

HttpHelper爬虫类(V2.0) 开源的爬虫类,支持多种模式和属性 源码 - 代码生成器 - 讨论区 - 教程- 例子

查看: 3589|回复: 3

[Asp.Net] 关于利用URLRewriter重写实现伪二级域名

[复制链接]
发表于 2013-1-13 08:33:30 | 显示全部楼层 |阅读模式
本帖最后由 youhaoxinqin 于 2013-1-13 08:35 编辑

利用URLRewriter重写实现伪二级域名
最近公司有个项目,要求将请求的参数放到放置到网址的前面
  例:原请求网址:www.abc.com?jc=dfs 改写层 dfs.abc.com,实现这种伪的二级域名。着实下了一番功夫,今天我就在这总结下。
第一步:下载一个URLRewriter。
  微软官方 关于URLRewriter的解释
  http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx
  进入以后可以下载源代码,当然也可以去别的地方下载别人修改过的代码。要安装,安装完毕以后,在“我的文档”→“MSDN”→“URL Rewriting in ASP.NET” →“URLRewritingCode.sln”   
第二步:要对URLRewriter里的方法重写
  这里我们要重写的,就是URLRewriter程序集下的两个文件 “BaseModuleRewriter.cs”和“ModuleRewriter.cs”
  1.BaseModuleRewriter.cs
[code=csharp]/// <summary>
        /// Called when the module's AuthorizeRequest event fires.
        /// </summary>
        /// <remarks>This event handler calls the <see cref="Rewrite"/> method, passing in the
        /// <b>RawUrl</b> and HttpApplication passed in via the <b>sender</b> parameter.</remarks>
        protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication) sender;
            Rewrite(app.Request.Path, app);
        }[/code]
 改为[code=csharp]/// <summary>
        /// Called when the module's AuthorizeRequest event fires.
        /// </summary>
        /// <remarks>This event handler calls the <see cref="Rewrite"/> method, passing in the
        /// <b>RawUrl</b> and HttpApplication passed in via the <b>sender</b> parameter.</remarks>
        protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
        {
            HttpApplication app = (HttpApplication) sender;
            //Rewrite(app.Request.Path, app);
            Rewrite(app.Request.Url.AbsoluteUri, app);
        }
[/code]
  2.ModuleRewriter.cs
[code=csharp]for(int i = 0; i < rules.Count; i++)
            {
                // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
                string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules.LookFor) + "$";
                // Create a regex (note that IgnoreCase is set...)
                Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);

                // See if a match is found
                if (re.IsMatch(requestedPath))
                {
                    // match found - do any replacement needed
                    string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules.SendTo));

                    // log rewriting information to the Trace object
                    app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);

                    // Rewrite the URL
                    RewriterUtils.RewriteUrl(app.Context, sendToUrl);
                    break;        // exit the for loop
                }
            }
[/code]
  改为
  1. for(int i = 0; i < rules.Count; i++)
  2.             {
  3.                 // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
  4.                 //string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";
  5.                 string lookFor = "^" + rules[i].LookFor + "$";
  6.                 // Create a regex (note that IgnoreCase is set...)
  7.                 Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);

  8.                 // See if a match is found
  9.                 if (re.IsMatch(requestedPath))
  10.                 {
  11.                     // match found - do any replacement needed
  12.                     string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));

  13.                     // log rewriting information to the Trace object
  14.                     app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);

  15.                     // Rewrite the URL
  16.                     RewriterUtils.RewriteUrl(app.Context, sendToUrl);
  17.                     break;        // exit the for loop
  18.                 }
  19.             }
复制代码
修改完成以后,重新生成,在bin文件下找到Debug文件夹中找到URLRewriter.dll这个文件。  在自己的项目中引用这个DLL文件
第三步:在自己的项目的web.config文件中坐下配置,就可以了
  1.在<configSections>节点下添加
    <section name="RewriterConfig" type="URLRewriter.Config.RewriterConfigSerializerSectionHandler, URLRewriter" />
  2.在<configuration>节点(即根目录的节点下)
    <RewriterConfig>
        <Rules>
          <RewriterRule>
            <LookFor>http://(\w+).abc.com/</LookFor>
            <SendTo>/Index.aspx?jv=$1</SendTo>
          </RewriterRule>
        </Rules>
      </RewriterConfig>
    如果有多个匹配地址可以写多个,<Rules>节点中多写几个<RewriterRule> 就可以了!
  3.在<system.web>节点下<httpModules>节点中添加
    <add type="URLRewriter.ModuleRewriter, URLRewriter" name="ModuleRewriter" />
  这样,基本就配置完毕了!
第四部:如果要自己测试,要把程序放到IIS服务器上,进行测试。要自己修改下hosts文件,写个虚假的域名指向程序!
//*********代码创 建 人:陈坤                                        ********//
//*********联 系  方 式:QQ:417643479 邮箱:youhaoxinqin@sina.com     ********//
//*********代码创建时间:2013-1-13 8:32                              ********//
//*********代码主要功能:                                              ********//
//*********代 码 版  本:                                            ********//
//*********代码维护时间:                                            ********//



1. 开通SVIP会员,免费下载本站所有源码,不限次数据,不限时间
2. 加官方QQ群,加官方微信群获取更多资源和帮助
3. 找站长苏飞做网站、商城、CRM、小程序、App、爬虫相关、项目外包等点这里
发表于 2013-1-13 14:06:55 | 显示全部楼层
顶起
发表于 2013-1-13 19:01:22 | 显示全部楼层
不错支持
发表于 2013-1-14 10:58:44 | 显示全部楼层
给力 mark
您需要登录后才可以回帖 登录 | 马上注册

本版积分规则

QQ|手机版|小黑屋|手机版|联系我们|关于我们|广告合作|苏飞论坛 ( 豫ICP备18043678号-2)

GMT+8, 2024-5-3 01:13

© 2014-2021

快速回复 返回顶部 返回列表