分类

链接

2018 年 3 月
 1234
567891011
12131415161718
19202122232425
262728293031  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > .NET > 正文
共享办公室出租
asp.net mvc静态化
.NET 暂无评论 阅读(427)

 

 

 

 

  1. public class StaticFileFilterAttribute : FilterAttribute, IResultFilter
  2.     {
  3.         public void OnResultExecuted(ResultExecutedContext filterContext)
  4.         {
  5.             filterContext.HttpContext.Response.Filter = new StaticFileWriteResponseFilterWrapper(filterContext.HttpContext.Response.Filter, filterContext);
  6.         }
  7.  
  8.         public void OnResultExecuting(ResultExecutingContext filterContext)
  9.         {
  10.  
  11.         }
  12.  
  13.         class StaticFileWriteResponseFilterWrapper : System.IO.Stream
  14.         {
  15.             private string path = "";
  16.             private bool cached = false;
  17.  
  18.             private System.IO.Stream inner;
  19.             private ControllerContext context;
  20.             public StaticFileWriteResponseFilterWrapper(System.IO.Stream s, ControllerContext context)
  21.             {
  22.                 this.inner = s;
  23.                 this.context = context;
  24.                 this.path = context.HttpContext.Server.MapPath(HttpContext.Current.Request.Path);
  25.             }
  26.  
  27.             public override bool CanRead
  28.             {
  29.                 get { return inner.CanRead; }
  30.             }
  31.  
  32.             public override bool CanSeek
  33.             {
  34.                 get { return inner.CanSeek; }
  35.             }
  36.  
  37.             public override bool CanWrite
  38.             {
  39.                 get { return inner.CanWrite; }
  40.             }
  41.  
  42.             public override void Flush()
  43.             {
  44.                 if (cached == false)
  45.                 {
  46.                     File.AppendAllText(path, "\r\n<!--cache by filter at " + DateTime.Now.ToString() + "-->");
  47.                     cached = true;
  48.                 }
  49.  
  50.                 inner.Flush();
  51.             }
  52.  
  53.             public override long Length
  54.             {
  55.                 get { return inner.Length; }
  56.             }
  57.  
  58.             public override long Position
  59.             {
  60.                 get
  61.                 {
  62.                     return inner.Position;
  63.                 }
  64.                 set
  65.                 {
  66.                     inner.Position = value;
  67.                 }
  68.             }
  69.  
  70.             public override int Read(byte[] buffer, int offset, int count)
  71.             {
  72.                 return inner.Read(buffer, offset, count);
  73.             }
  74.  
  75.             public override long Seek(long offset, System.IO.SeekOrigin origin)
  76.             {
  77.                 return inner.Seek(offset, origin);
  78.             }
  79.  
  80.             public override void SetLength(long value)
  81.             {
  82.                 inner.SetLength(value);
  83.             }
  84.  
  85.  
  86.  
  87.             public override void Write(byte[] buffer, int offset, int count)
  88.             {
  89.                 inner.Write(buffer, offset, count);
  90.  
  91.                 try
  92.                 {
  93.  
  94.                     if (Path.HasExtension(path))
  95.                     {
  96.                         string dir = Path.GetDirectoryName(path);
  97.                         if (!Directory.Exists(dir))
  98.                         {
  99.                             Directory.CreateDirectory(dir);
  100.                         }
  101.                         //if (File.Exists(p))
  102.                         //{
  103.                         //    File.Delete(p);
  104.                         //}
  105.                         File.AppendAllText(path, System.Text.Encoding.UTF8.GetString(buffer, offset, count));
  106.                     }
  107.                 }
  108.                 catch (Exception ex)
  109.                 {
  110.  
  111.                 }
  112.  
  113.  
  114.             }
  115.         }
  116.  
  117.     }

 

  1.  <system.webServer>
  2.     <validation validateIntegratedModeConfiguration="false" />
  3.     <modules runAllManagedModulesForAllRequests="false" />
  4.     <handlers>
  5.      ...
  6.       <add name="htmlHandler" verb="GET,HEAD" path="*.html" type="System.Web.StaticFileHandler"/>
  7.     </handlers>
  8.   </system.webServer>

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:asp.net mvc静态化 | Bruce's Blog

发表评论

留言无头像?