zoukankan      html  css  js  c++  java
  • ASP.NET中实现二级或多级域名(修改UrlRewrite) (转)

    大家应该知道,微软的URLRewrite能够对URL进行重写,但是也只能对域名之后的部分进行重写,而不能对域名进行重写,如:可将 http://http://www.abc.com//1234/  重写为 http://www.abc.com/show.aspx?id=1234  但不能将
    http://1234.abc.com/  重写为  http://www.abc.com/show.aspx?id=1234

    要实现这个功能,前提条件就是  http://www.abc.com/ 是泛解析的,再就是要修改一下URLRewriter了。
    总共要修改2个文件

    1.BaseModuleRewriter.cs

    protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
            
    {
                HttpApplication app 
    = (HttpApplication) sender;
                Rewrite(app.Request.Path, app);
            }

    改为

    protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e)
            
    {
                HttpApplication app 
    = (HttpApplication) sender;
                Rewrite(app.Request.Url.AbsoluteUri, app);
            }


    就是将  app.Request.Path 替换成了  app.Request.Url.AbsoluteUri

    2.ModuleRewriter.cs

    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[i].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[i].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
                    }

                }

    改为

    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 = "^" + rules[i].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[i].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
                    }

                }



    string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";

    改成了

    string lookFor = "^" + rules[i].LookFor + "$";


    完成这2处改动之后重新编译项目,将生成的dll复制到bin目录下。

    再就是写web.config里的重写正则了

    <RewriterRule>
                
    <LookFor>http://(\d+)\.abc\.com/</LookFor>
                
    <SendTo>/show.aspx?id=$1</SendTo>
            
    </RewriterRule>


    好了大功告成,你在IE地址栏输入http://1234.abc.com/,就可以看到http://www.abc.com/show.aspx?id=1234

    的结果了

    若你在实际应用中碰到了问题,请查看文章 "修改UrlRewrite以对域名进行重写"需要注意的问题 ,希望能够帮助你!


    附:

    URLRewriter 的相关资料

    http://www.microsoft.com/china/msdn/library/webservices/asp.net/URLRewriting.mspx
  • 相关阅读:
    宋宝华:slab在内核内存管理和用户态Memcached的双重存在
    能感知功耗的Linux调度器(EAS)
    内存检测王者之剑—valgrind
    随心所动,厂商的CPU核管理策略介绍
    一文读懂 进程怎么绑定 CPU
    Fastbootd实现原理分析
    cachestat、cachetop、pcstat-linux系统缓存命中率分析工具
    WIFI的WPS和pin码(测试失败)
    视频下载(钉钉、B站等) 解决方案
    DevExpress 报表设计文件(.vsrepx)不显示或显示空白
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/1504324.html
Copyright © 2011-2022 走看看