ASP. net mvc optimization notes-

Source: Internet
Author: 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: Can I enter about: cache in the browser? Device = disk: view the browser Cache data. ** 1. Add ** Response. Cache. SetOmitVaryStar (true) to the Action. * This prevents unstable Cache. 200 and 304 may occur occasionally. * By: <Wang Kun> */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> /// server cache 30 S /// </summary> /// <returns> </returns> [OutputCache (Duration = 30, Location = OutputCacheLoc Ation. serverAndClient)] public ActionResult ServerCache () {Response. cache. setOmitVaryStar (true); ViewBag. time = DateTime. now; return View ();} /** configure the cache time through the configuration file <caching> <outputCacheSettings> <outputCacheProfiles> <add name = "Cache1Hour" duration = "60" varyByParam = "none"/> </outputCacheProfiles> </outputCacheSettings> </caching> *: <Wang Kun> */[OutputCache (CacheProfile = "Cache1 Hour ", 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> /// output compression /// </summary> [AttributeUsage (AttributeTargets. class | AttributeTargets. M Ethod)] public class CompressFilter: ActionFilterAttribute {public override void OnActionExecuting (ActionExecutingContext filterContext) {/* OnActionExecuting in the ActionFilterAttribute class, nothing is actually just a blank virtual method. Base. onActionExecuting (filterContext); * By: <Wang Kun> */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 );}}}}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

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.