ASP.NET MVC 最佳化筆記 -

來源:互聯網
上載者:User
using System;using System.Collections.Generic;using System.IO.Compression;using System.Linq;using System.Web;using System.Web.Mvc;using System.Web.UI;namespace MvcAppCache.Controllers{    /*     *      * FireFox:可以在瀏覽器中輸入 about:cache?device=disk 查看瀏覽器快取資料     *      * 1. 在 Action 中添加:     *  Response.Cache.SetOmitVaryStar(true);     *  防止緩衝不穩定  200 與 304  不定期出現。     * By: Rhythmk.cnblogs.com <王坤>     */    public class HomeController : Controller    {        //        // GET: /Home/        [OutputCache(Duration = 30, Location = OutputCacheLocation.ServerAndClient)]        public ActionResult Index()        {                        Response.Cache.SetOmitVaryStar(true);            ViewBag.Time = DateTime.Now;            return View();        }        /// <summary>        ///  服務端緩衝30S        /// </summary>        /// <returns></returns>        [OutputCache(Duration=30,Location=OutputCacheLocation.ServerAndClient)]        public ActionResult ServerCache()        {            Response.Cache.SetOmitVaryStar(true);            ViewBag.Time = DateTime.Now;            return View();        }        /*         *     通過設定檔配置緩衝時間                <caching>                     <outputCacheSettings>                        <outputCacheProfiles>                            <add name="Cache1Hour" duration="60" varyByParam="none"/>                        </outputCacheProfiles>                    </outputCacheSettings>                </caching>         * By: Rhythmk.cnblogs.com <王坤>         */        [OutputCache(CacheProfile = "Cache1Hour", Location = OutputCacheLocation.Client)]        public ActionResult ConfigCache()        {            Response.Cache.SetOmitVaryStar(true);            ViewBag.Time = DateTime.Now;            return View();        }        [CompressFilter]        public ActionResult Compress()        {            return View();        }        public ActionResult NoCompress()        {            return PartialView("Compress");        }    }    /// <summary>    /// 輸出壓縮    /// </summary>    [AttributeUsage(AttributeTargets.Class|AttributeTargets.Method)]    public class CompressFilter : ActionFilterAttribute    {        public override void OnActionExecuting(ActionExecutingContext filterContext)        {            /*           OnActionExecuting 在 ActionFilterAttribute 類中其實什麼都沒有做  僅僅是一個空的虛方法。           base.OnActionExecuting(filterContext);             * By: Rhythmk.cnblogs.com <王坤>           */            HttpRequestBase request = filterContext.HttpContext.Request;            string acceptEncoding = request.Headers["Accept-Encoding"];            if (string.IsNullOrEmpty(acceptEncoding)) return;            acceptEncoding = acceptEncoding.ToUpperInvariant();            HttpResponseBase response = filterContext.HttpContext.Response;            if (acceptEncoding.Contains("GZIP"))            {                response.AppendHeader("Content-encoding", "gzip");                response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);            }            else if (acceptEncoding.Contains("DEFLATE"))            {                response.AppendHeader("Content-encoding", "deflate");                response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);            }                   }           }}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.