原文地址:http://hongzhang.me/post/2011/06/11/godaddy-blogengine-virtualpath.aspx

Godaddy的虚拟主机支持多个域名,但是我再配置BlogEngine.NET的时候,发现生成的URL包含虚拟目录,如:http://mydomain/blog/...... 红色的blog就是我再主机上面建立的子目录,用于存放BlogEngine.NET的目录。如何不让blog显示在生成的URL中呢?

如果修改源代码,那么以后的升级会很麻烦。所以解决方法必须要做到可配置,对源代码修改最小化。我的思路是添加一个HttpModule,在 HttpModule中将所有的a标签里面包含虚拟目录名称的替换成空字符串。这样对源代码的修改只是在web.config里面多注册一个 HttpModule。

方法确定了,有个关键问题需要解决:如何在HttpModule中修改Http输出内容?方法就是使用HttpApplication.Response.Filter,自定义一个Filter,在Filter里面实现URL的分析和处理。

关键代码:

1.定义HttpModule

public   class  VitualPathHttpModule:IHttpModule
    {
         public   void  Dispose()
        {
            
        }

public   void  Init(HttpApplication context)
        {
            context.ReleaseRequestState  +=   new  EventHandler(context_ReleaseRequestState);
        }

void  context_ReleaseRequestState( object  sender, EventArgs e)
        {
            HttpApplication application  =  (HttpApplication)sender;

// 装配过滤器
            application.Response.Filter  =   new  VirtualPathFilter(application.Response.Filter, application);

}
    }

2.定义自定义Filter:VirtualPathFilter

public   class  VirtualPathFilter : Stream
    {
        Stream responseStream;
         long  position;
        HttpApplication context;

private  VirtualPathFilter()
        {
        }

public  VirtualPathFilter(Stream inputStream,HttpApplication inputContext)
        {
            responseStream  =  inputStream;
            context  =  inputContext;
        }

#region  Write
        
         public   override   void  Write( byte [] buffer,  int  offset,  int  count)
        {
             string  strContent  =  UTF8Encoding.UTF8.GetString(buffer);

string  vitualPath  =  VirtualPathUtility.ToAbsolute(ConfigurationManager.AppSettings[ " BlogEngine.VirtualPath " ]);
             if  ( ! string .IsNullOrEmpty(vitualPath))
            {               
                var vitualPath1  =  vitualPath.TrimStart( ' / ' );
                var vitualPath2  =  vitualPath.TrimEnd( ' / ' );
                var vitualPath3  =  vitualPath.Trim( ' / ' );
                
                 string  pattern  =   " <a[^<>]+href=\"[^>\"]* " +  vitualPath3  + " [^>\"]*\" " ;
                Regex reg  =   new  Regex(pattern, RegexOptions.IgnoreCase);
                var ms  =  reg.Matches(strContent);
                List < string >  values  =   new  List < string > ();
                 foreach  (Match item  in  ms)
                {
                    var value  =  item.Value;
                     if  (values.Contains(value))
                    {
                         continue ;
                    }
                    values.Add(value);
                     if  (value.IndexOf(vitualPath)  >   - 1 )
                    {
                        value  =  value.Replace(vitualPath,  " / " );
                    }
                     if  (value.IndexOf(vitualPath1)  >   - 1 )
                    {
                         //  othersite/blog/#NET -> #NET                
                        value  =  value.Replace(vitualPath1,  "" );
                    }

if  (value.IndexOf(vitualPath2)  >   - 1 )
                    {
                         //  /othersite/blog -> /
                        value  =  value.Replace(vitualPath2,  " / " );
                    }

if  (value.IndexOf(vitualPath3)  >   - 1 )
                    {
                        value  =  value.Replace(vitualPath3,  "" );
                    }
                    strContent  =  strContent.Replace(item.Value, value);
                }

}
           
            responseStream.Write(UTF8Encoding.UTF8.GetBytes(strContent), offset, UTF8Encoding.UTF8.GetByteCount(strContent));
            
        }
         #endregion     
    
        
         #region  Filter Overrides

public   override   bool  CanRead
        {
             get
            {
                 return   true ;
            }
        }

public   override   bool  CanSeek
        {
             get
            {
                 return   true ;
            }
        }

public   override   bool  CanWrite
        {
             get
            {
                 return   true ;
            }
        }
         public   override   void  Close()
        {
            responseStream.Close();
        }

public   override   void  Flush()
        {
            responseStream.Flush();
        }

public   override   long  Length
        {
             get
            {
                 return   0 ;
            }
        }

public   override   long  Position
        {
             get
            {
                 return  position;
            }
             set
            {
                position  =  value;
            }
        }

public   override   int  Read( byte [] buffer,  int  offset,  int  count)
        {
             return  responseStream.Read(buffer, offset, count);
        }

public   override   long  Seek( long  offset, SeekOrigin origin)
        {
             return  responseStream.Seek(offset, origin);
        }

public   override   void  SetLength( long  length)
        {
            responseStream.SetLength(length);
        }
         #endregion

}

3.在Web.config中注册HttpModule

< modules >
< add  name ="WwwSubDomainModule"  type ="BlogEngine.Core.Web.HttpModules.WwwSubDomainModule, BlogEngine.Core" />
     < add  name ="UrlRewrite"  type ="BlogEngine.Core.Web.HttpModules.UrlRewrite, BlogEngine.Core" />
     < add  name ="CompressionModule"  type ="BlogEngine.Core.Web.HttpModules.CompressionModule, BlogEngine.Core" />
     < add  name ="ReferrerModule"  type ="BlogEngine.Core.Web.HttpModules.ReferrerModule, BlogEngine.Core" />
       < add  name ="VitualPathModule"  type ="BE.Extention.VitualPathHttpModule,BE.Extention" />
</ modules >

至此,BlogEngine.NET生成的URL里面就不包含虚拟目录了,大功告成。

转载于:https://www.cnblogs.com/hunk/archive/2011/06/18/2084398.html

在Godday虚拟主机上面配置BlogEngine.NET虚拟目录的处理方法相关推荐

  1. 虚拟主机的配置及原理

    虚拟主机的配置及原理 虚拟主机是指在网络服务器上分出一定的磁盘空间,用户可以租用此部分空间,以供 用户放置站点及应用组件,提供必要的数据存放和传输功能. 虚拟主机技术的出现,是对Internet技术的 ...

  2. Nginx教程--02.Nginx虚拟主机的配置

    1.Nginx虚拟主机的配置 1.1 在conf目录下,使用命令 : vim nginx.conf 对上图解释: //全局区 worker _processes 1; //表示当前有1个工作的子进程, ...

  3. apache 虚拟主机如何配置

    apache 虚拟主机如何配置?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题.  apache虚拟主机就是在apache服务器上配置多个虚 ...

  4. Apache 虚拟主机 VirtualHost 配置

    虚拟主机 (Virtual Host) 是在同一台机器搭建属于不同域名或者基于不同 IP 的多个网站服务的技术. 可以为运行在同一物理机器上的各个网站指配不同的 IP 和端口, 也可让多个网站拥有不同 ...

  5. linux虚拟主机泛解析,Apache虚拟主机的配置和泛域名解析实现代码

    Apache虚拟主机的配置和泛域名解析实现代码 更新时间:2012年03月11日 00:28:19   作者: Apache虚拟主机的配置和泛域名解析实现代码,需要的朋友可以参考下 虚拟主机的配置 基 ...

  6. apache 虚拟主机详细配置:http.conf配置详解

    Apache的配置文件http.conf参数含义详解 Apache的配置由httpd.conf文件配置,因此下面的配置指令都是在httpd.conf文件中修改. 主站点的配置(基本配置) (1) 基本 ...

  7. Apache 虚拟主机的配置[Ubuntu]

    == 基本配置 == 我们都知道,如果我们想在单台机器上设置多个域名或主机名时,我们就要用到基于名称的虚拟主机了.那么要如何进行设置呢?这就是本 HowTo 想解决的问题了.在 Ubuntu 的 /e ...

  8. win2003服务器 虚拟主机安全配置

    win2003服务器 虚拟主机安全配置 注入漏洞.上传漏洞.弱口令漏洞等问题随处可见.跨站***,远程控制等等是再老套不过了的话题.有些虚拟主机管理员不知是为了方便还是不熟悉配置,干脆就将所有的网站都 ...

  9. php 5 在虚拟主机上安装,Apache多虚拟主机多版本PHP(5.2+5.3+5.4)共存运行配置全过程...

    因为某种需求,可能是因为早期的项目需要低版本的php,和目前开发所用的版本不太一致,我们需要给不同的虚拟主机配置不同版本的PHP.避免去额外配置多个Apache,等iis和apache共存的麻烦. 下 ...

最新文章

  1. C++11中weak_ptr的使用
  2. 收藏长文|Java 代码精简之道
  3. 将python编程为c_使用Cython为Python编写更快的C扩展
  4. 探测服务器操作系统,探测服务器操作系统工具
  5. Hangfire入门(任务调度)
  6. ivx中字体显示_html-中文字体在CSS中的显示(Unicode编码)(转载)
  7. 蒙层禁止页面滚动的方案
  8. Python实现图像空域随机水印加入与提取
  9. centos rpm安装zabbix
  10. Java第十周的作业
  11. 科多大数据_redis缓存的简单使用—科多大数据
  12. 使用树莓派 Raspberry Pi 播放豆瓣 FM
  13. 电工模拟接线软件 app_超全的电工接线方法口诀
  14. 解决微信、QQ在一加手机系统中无法限制后台的问题
  15. Minecraft Mod安装教程
  16. SSL_WRITE在断网时的表现
  17. SuperMap地图发布
  18. 网站劫持 网站(域名)被劫持怎么检测 遇到网站恶意跳转不要慌(干货)
  19. MemSQL学习笔记-类似MySQL的数据库
  20. Vue父组件mounted执行完后再执行子组件mounted执行顺序问题

热门文章

  1. 为什么会信号无服务器,为什么在高铁上会无服务,无信号呢?
  2. Golang常用设计模式
  3. linux命令赋子包777权限,linux对文件赋权限的命令chmod的详细说明
  4. div absolute 居中
  5. [POJ1873]The Fortified Forest(dfs+凸包)
  6. Construct binary tree from preorder and inoder--LeetCode
  7. CTR预估模型DSSM
  8. docker部署wizard
  9. ANDROID N 在SetupWizard后加入自己的页面
  10. 使用独立显卡安装黑苹果,但无法扩展显示器的解决方案