分享

巧用asp.net 过滤所有的Response请求并替换部分内容,彻底解决MVC虚拟路径问题.

 icecity1306 2015-05-11

巧用asp.net 过滤所有的Response请求并替换部分内容,彻底解决MVC虚拟路径问题.
如题:
我的项目需要用到mvc,但是用习惯了asp.net的aspx文件形式,用mvc还真有点不习惯,
首当其冲的就是路径问题...
众所周知,
mvc最大的特点就是重用,重用页面.重用控制器..
但是比较郁闷的是,页面中的文件引用,图片路径问题真是无法解决.
用/代表的是网站的根目录.. 注意是浏览器的urlwww.***.com/中的这第一个/
用./和../代表的是相对路径.但是这路径也是相对浏览器的url来指定的, 比如 www.***.com/Home/Index指向Index.aspx, 其中写了一句话

<img src="./images/a.gif" alt="" />

如果Index.aspx被另外一个 www.***.com/Home/User/Index 路径引用

这个图片将失效.因为只要是在客户端的路径都是相对浏览器url的.
所以我们的Index.aspx将不可重用..
但是折中的方法就是把Index.aspx中的这句话改成

<img src="/images/a.gif" alt="" />

这样做重用问题算是解决了,可是还有一个致命的虚拟目录问题..
如果把你的网站移植到一个子目录下.则整个程序将全部是错误..

所以最好的方法就是把路径全部相对于你的项目..从你的项目根目录开始算起..这样可移植性就高很多.

但是,浏览器根本不知道你的项目根目录是那一层..
我们知道asp.net提供了从根目录开始的方法, ~/
~/指向的就是当前项目的根目录..
不过比较悲剧这个~/只只能在服务器端用.. 客户端根本不认识~/
所以就有了下文..
我在服务器端,把所有的输出都过滤一遍,如果输出的文本中包含 ~/  就把 ~/  替换成 项目在网站中的从根目录 /  开始的绝对路径
所以以后的写法就是

<img src="~/images/a.gif" alt="" />

以后你的项目就随便移植和重用吧..哈哈..

当然别忘啦所有的路径都从 ~/  开始哦

 

 

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.IO;  
  6. using System.Web;  
  7.   
  8. namespace HttpModules  
  9. {  
  10.   
  11.     public class ResponseFilter : IHttpModule  
  12.     {  
  13.         private string vriualRootPath;  
  14.         void IHttpModule.Dispose() { }  
  15.   
  16.   
  17.         void IHttpModule.Init(HttpApplication context)  
  18.         {  
  19.             context.BeginRequest += (sender, e) =>  
  20.             {  
  21.                 var f = new RootPathReplaceStream(context.Response.Filter);  
  22.                 context.Response.Filter = f;  
  23.             };  
  24.         }  
  25.     }  
  26.   
  27.     public class RootPathReplaceStream : Stream  
  28.     {  
  29.   
  30.         private Stream output;  
  31.         public RootPathReplaceStream(Stream s)  
  32.         {  
  33.             output = s;  
  34.         }  
  35.         public override bool CanRead  
  36.         {  
  37.             get { return output.CanRead; }  
  38.         }  
  39.   
  40.         public override bool CanSeek  
  41.         {  
  42.             get { return output.CanSeek; }  
  43.         }  
  44.   
  45.         public override bool CanWrite  
  46.         {  
  47.             get { return output.CanWrite; }  
  48.         }  
  49.   
  50.         public override void Flush()  
  51.         {  
  52.             output.Flush();  
  53.         }  
  54.   
  55.         public override long Length  
  56.         {  
  57.             get { return output.Length; }  
  58.         }  
  59.   
  60.         public override long Position  
  61.         {  
  62.             get { return output.Position; }  
  63.             set { output.Position = value; }  
  64.         }  
  65.   
  66.         public override int Read(byte[] buffer, int offset, int count)  
  67.         {  
  68.             return output.Read(buffer, offset, count);  
  69.         }  
  70.   
  71.         public override long Seek(long offset, SeekOrigin origin)  
  72.         {  
  73.             return output.Seek(offset, origin);  
  74.         }  
  75.   
  76.         public override void SetLength(long value)  
  77.         {  
  78.             output.SetLength(value);  
  79.         }  
  80.   
  81.         public override void Write(byte[] buffer, int offset, int count)  
  82.         {  
  83.             StringComparison ignore = StringComparison.CurrentCultureIgnoreCase;  
  84.             if (HttpContext.Current != null)  
  85.             {  
  86.                 HttpContext context = HttpContext.Current;  
  87.                 if (context.Response.ContentType.Equals("text/html", ignore))  
  88.                 {  
  89.                     Encoding encoding = context.Response.ContentEncoding;  
  90.   
  91.                     //在這邊把 google 換成 microsoft  
  92.                     string html = encoding.GetString(buffer, offset, count);  
  93.                     //    html.Replace("google", "microsoft");  
  94.   
  95.                     //做自己的处理       
  96.                     //註冊事件,在 BeginRequest 的時候把 Response.Filter 換掉  
  97.   
  98.   
  99.                     //此种替换方法不太严谨  
  100.                     //html = html.Replace("~/",  VriualRootPath);  
  101.                     //替换 "~/ 为虚拟路径  这样只替换带"号的目录   
  102.                     html = html.Replace("\"~/""\"" + VriualRootPath);  
  103.                     //替换  '~/ 为虚拟路径 这样只替换带'号的目录   
  104.                     html = html.Replace("'~/""'" + VriualRootPath);  
  105.   
  106.                     byte[] bytes = encoding.GetBytes(html);  
  107.                     output.Write(bytes, 0, bytes.Length);  
  108.                 }  
  109.                 else  
  110.                 {  
  111.                     output.Write(buffer, offset, count);  
  112.                 }  
  113.             }  
  114.         }  
  115.   
  116.         public static string _VriualRootPath;  
  117.   
  118.         public  string VriualRootPath  
  119.         {  
  120.             get  
  121.             {  
  122.                 if (_VriualRootPath == null)  
  123.                 {  
  124.                     //计算当前虚拟目录的相对路径  
  125.                     string absRootPath = HttpContext.Current.Server.MapPath("/");  
  126.                     _VriualRootPath = HttpContext.Current.Server.MapPath("~/");  
  127.   
  128.                     _VriualRootPath = VriualRootPath.Replace(absRootPath, "/");  
  129.                     _VriualRootPath = VriualRootPath.Replace("\\", "/");// \ 变成 /   
  130.                 }  
  131.                 return _VriualRootPath;  
  132.             }  
  133.             set {  
  134.                 _VriualRootPath = value;  
  135.             }  
  136.         }  
  137.     }  
  138.   
  139. }  

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多