MVC静态化页面

来源:互联网 发布:华为云计算 钦州 编辑:程序博客网 时间:2024/05/21 17:39
[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.IO;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Web;  
  7. using System.Web.Mvc;  
  8.   
  9. namespace WF_HightFood.App_Start  
  10. {  
  11.   
  12.     public class StaticFilterAttribute : ActionFilterAttribute  
  13.     {  
  14.         public override void OnActionExecuted(ActionExecutedContext filterContext)  
  15.         {  
  16.             //filterContext.HttpContext.Response.Write("OnActionExecuted</br>");  
  17.             base.OnActionExecuted(filterContext);  
  18.         }  
  19.   
  20.         public override void OnActionExecuting(ActionExecutingContext filterContext)  
  21.         {  
  22.             // filterContext.HttpContext.Response.Write("OnActionExecuting</br>");  
  23.             base.OnActionExecuting(filterContext);  
  24.         }  
  25.   
  26.         public override void OnResultExecuted(ResultExecutedContext filterContext)  
  27.         {  
  28.             //filterContext.HttpContext.Response.Write("OnResultExecuted</br>");  
  29.   
  30.             if (filterContext.HttpContext.Response.StatusCode == 200)  
  31.             {  
  32.                 filterContext.HttpContext.Response.Filter = new StaticFileWriteResponseFilterWrapper(filterContext.HttpContext.Response.Filter, filterContext);  
  33.             }  
  34.             // filterContext.HttpContext.Response.Charset = "utf8";  
  35.             base.OnResultExecuted(filterContext);  
  36.         }  
  37.   
  38.         public override void OnResultExecuting(ResultExecutingContext filterContext)  
  39.         {  
  40.             // filterContext.HttpContext.Response.Write("OnResultExecuting</br>");  
  41.             base.OnResultExecuting(filterContext);  
  42.         }  
  43.     }  
  44.     class StaticFileWriteResponseFilterWrapper : System.IO.Stream  
  45.     {  
  46.         private Stream inner;  
  47.         private FileStream writer;  
  48.         private ControllerContext context;  
  49.         private int expireSconds;  
  50.         private bool filter;  
  51.         private string tempPath, path;  
  52.   
  53.         public StaticFileWriteResponseFilterWrapper(System.IO.Stream s, ControllerContext context, int expireSeconds = 600)  
  54.         {  
  55.             this.filter = false;  
  56.             this.inner = s;  
  57.             this.context = context;  
  58.             this.expireSconds = expireSeconds;  
  59.             this.EnsureStaticFile();  
  60.         }  
  61.         void EnsureStaticFile()  
  62.         {  
  63.             this.path = this.context.HttpContext.Server.MapPath(HttpContext.Current.Request.Path);  
  64.   
  65.             if (!Path.HasExtension(path))  
  66.             {  
  67.                 return;  
  68.             }  
  69.             if (!".html".Equals(Path.GetExtension(HttpContext.Current.Request.Path)))  
  70.             {  
  71.                 return;  
  72.             }  
  73.   
  74.             if (File.Exists(path))  
  75.             {  
  76.                 var delay = DateTime.UtcNow - File.GetCreationTimeUtc(path);  
  77.                 if (delay.TotalSeconds <= this.expireSconds)  
  78.                 {  
  79.                     return;  
  80.                 }  
  81.                 File.Delete(path);  
  82.             }  
  83.             else  
  84.             {  
  85.                 var dir = Path.GetDirectoryName(path);  
  86.                 if (!Directory.Exists(dir))  
  87.                 {  
  88.   
  89.                     try  
  90.                     {  
  91.                         Directory.CreateDirectory(Path.GetDirectoryName(path));  
  92.                     }  
  93.                     catch  
  94.                     { }  
  95.                 }  
  96.             }  
  97.             this.filter = true;  
  98.   
  99.             this.tempPath = this.path + "_" + DateTime.Now.Ticks;  
  100.   
  101.             try  
  102.             {  
  103.                 writer = new FileStream(tempPath, FileMode.Create, FileAccess.Write);  
  104.             }  
  105.             catch  
  106.             {  
  107.                 this.filter = false;  
  108.             }  
  109.         }  
  110.   
  111.   
  112.   
  113.         public override bool CanRead  
  114.         {  
  115.             get { return inner.CanRead; }  
  116.         }  
  117.   
  118.         public override bool CanSeek  
  119.         {  
  120.             get { return inner.CanSeek; }  
  121.         }  
  122.   
  123.         public override bool CanWrite  
  124.         {  
  125.             get { return inner.CanWrite; }  
  126.         }  
  127.   
  128.         public override void Flush()  
  129.         {  
  130.             inner.Flush();  
  131.         }  
  132.   
  133.   
  134.   
  135.         public override long Length  
  136.         {  
  137.             get { return inner.Length; }  
  138.         }  
  139.   
  140.         public override long Position  
  141.         {  
  142.             get  
  143.             {  
  144.                 return inner.Position;  
  145.             }  
  146.             set  
  147.             {  
  148.                 inner.Position = value;  
  149.             }  
  150.         }  
  151.   
  152.         public override int Read(byte[] buffer, int offset, int count)  
  153.         {  
  154.             return inner.Read(buffer, offset, count);  
  155.         }  
  156.   
  157.         public override long Seek(long offset, System.IO.SeekOrigin origin)  
  158.         {  
  159.             return inner.Seek(offset, origin);  
  160.         }  
  161.   
  162.         public override void SetLength(long value)  
  163.         {  
  164.             inner.SetLength(value);  
  165.         }  
  166.   
  167.         public override void Write(byte[] buffer, int offset, int count)  
  168.         {  
  169.             try  
  170.             {  
  171.                 inner.Write(buffer, offset, count);  
  172.             }  
  173.             catch (Exception ex)  
  174.             {  
  175.             }  
  176.   
  177.             try  
  178.             {  
  179.                 this.writer.Write(buffer, offset, count);  
  180.             }  
  181.             catch (Exception ex)  
  182.             {  
  183.   
  184.             }  
  185.         }  
  186.   
  187.         protected override void Dispose(bool disposing)  
  188.         {  
  189.             if (this.filter)  
  190.             {  
  191.                 try  
  192.                 {  
  193.                     if (this.writer != null)  
  194.                     {  
  195.                         this.writer.Dispose();  
  196.                         this.writer = null;  
  197.                     }  
  198.   
  199.                     File.Delete(this.path);  
  200.                     File.Move(this.tempPath, this.path);  
  201.                     #region 生成文件日志  
  202.                     
  203.                     #endregion  
  204.                 }  
  205.                 catch  
  206.                 { }  
  207.   
  208.             }  
  209.             base.Dispose(disposing);  
  210.         }  
  211.     }  
  212. }  


[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Routing;  
  7. using EnYuan.SMS;  
  8. using YJY.Site.SSO;  
  9. using EnYuan.BSS.User;  
  10. using EnYuan.BSS.User.Dto;  
  11. using WF_HightFood.App_Code;  
  12.   
  13. namespace WF_HightFood.App_Start  
  14. {  
  15.     public class SSOFilterAttribute : ActionFilterAttribute  
  16.     {  
  17.   
  18.         public string Message { getset; }  
  19.   
  20.         public override void OnActionExecuting(ActionExecutingContext filterContext)  
  21.         {  
  22.             //if (!filterContext.HttpContext.Request.Url.ToString().ToLower().StartsWith("http://www."))  
  23.             //{  
  24.             //    filterContext.HttpContext.Response.Redirect(filterContext.HttpContext.Request.Url.ToString().Replace("http://", "http://www."));  
  25.             //    return;  
  26.             //}  
  27.             var sso_cookies = filterContext.HttpContext.Request.Cookies["sso_token"];  
  28.             if (sso_cookies == null || sso_cookies.Value == null || sso_cookies.Value.Equals(""))  
  29.             {  
  30.                 //清空所有cookies  
  31.                 filterContext.HttpContext.Request.Cookies.Clear();  
  32.                 //如果不存在token,跳转到验证站点进行验证;  
  33.                 filterContext.HttpContext.Response.Redirect("http://jump.yuan.cn/Home/index/?type=mvc&backurl=" + filterContext.HttpContext.Request.Url.ToString());  
  34.                 return;  
  35.             }  
  36.             else  
  37.             {  
  38.                 //如过存在token,检测登录状态  
  39.                 var userid = ServiceLocator.Create<ISSOService>().CheckUser(sso_cookies.Value);  
  40.                 if (userid != null)  
  41.                 {  
  42.                     if (filterContext.HttpContext.Session["userid"] != userid || filterContext.HttpContext.Session["userid"] == null || filterContext.HttpContext.Session["isVip"] == null || filterContext.HttpContext.Session["username"] == null)  
  43.                     {  
  44.                         //用户id  
  45.                         filterContext.HttpContext.Session["userid"] = userid;  
  46.                         //取得用户对象  
  47.                         UserDto userInfo = ServiceLocator.Create<IUserService>().GetUser(userid);  
  48.                         filterContext.HttpContext.Session["isVip"] = DataAccess.isVip(userid);  
  49.                         filterContext.HttpContext.Session["username"] = !String.IsNullOrEmpty(userInfo.NickName) ? userInfo.NickName : userInfo.Name;  
  50.                     }  
  51.                 }  
  52.                 else  
  53.                 {  
  54.                     filterContext.HttpContext.Session.Clear();  
  55.                 }  
  56.             }  
  57.         }  
  58.   
  59.         public override void OnActionExecuted(System.Web.Mvc.ActionExecutedContext filterContext)  
  60.         {  
  61.             base.OnActionExecuted(filterContext);  
  62.             // filterContext.HttpContext.Response.Write("Action执行之后" + Message + "<br />");  
  63.         }  
  64.   
  65.         public override void OnResultExecuting(ResultExecutingContext filterContext)  
  66.         {  
  67.             base.OnResultExecuting(filterContext);  
  68.             //filterContext.HttpContext.Response.Write("返回Result之前" + Message + "<br />");  
  69.         }  
  70.   
  71.         public override void OnResultExecuted(ResultExecutedContext filterContext)  
  72.         {  
  73.             base.OnResultExecuted(filterContext);  
  74.             // filterContext.HttpContext.Response.Write("返回Result之后" + Message + "<br />");  
  75.         }  
  76.     }  
  77. }  

使用方法:

 [StaticFilter]
        public ActionResult Index()
        {

 return View();

}


http://blog.csdn.net/lybwwp/article/details/26503765

0 0
原创粉丝点击