GZip
public class CompressContentAttribute : ActionFilterAttribute{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
var response = HttpContext.Current.Response;
if (response.IsRequestBeingRedirected) return;
var encoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
var isSupported = !string.IsNullOrEmpty(encoding) && (encoding.Contains("gzip") || encoding.Contains("deflate"));
if (isSupported)
{
var acceptEncoding = HttpContext.Current.Request.Headers["Accept-Encoding"];
if (acceptEncoding.Contains("gzip"))
{
response.Filter = new System.IO.Compression.GZipStream(response.Filter, System.IO.Compression.CompressionMode.Compress);
response.Headers.Remove("Content-Encoding");
response.AppendHeader("Content-Encoding", "gzip");
}
else
{
response.Filter = new System.IO.Compression.DeflateStream(response.Filter, System.IO.Compression.CompressionMode.Compress);
response.Headers.Remove("Content-Encoding");
response.AppendHeader("Content-Encoding", "deflate");
}
}
response.AppendHeader("Vary", "Content-Encoding");
}
}
// use
[CompressContent]
public ActionResult Details()
{
return this.RazorView();
}
============================
use binders
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Optimization;
namespace TownSuiteWebPortals.App_Start
{
public class BundleConfig
{
public static void RegisterBundles(BundleCollection bundles)
{
bundles.Add(new ScriptBundle("~/bundles/loginpagescript").Include(
"~/assets/javascript/notify.min.js",
"~/assets/javascript/Encode-Decode.js",
"~/assets/typescript/helpers_ts.js",
"~/assets/typescript/tsmessage.js"));
bundles.Add(new Bundle("~/bundles/loginpagestyle").Include(
"~/assets/externals/tsbootstrap/tsbootstrap/app/css/bootstrap.css",
"~/assets/externals/tsbootstrap/tsbootstrap/app/css/app.css"));
}
}
}
============= use
.cshtml
@Styles.Render("~/bundles/loginpagestyle")
@Scripts.Render("~/bundles/loginpagescript")
No comments:
Post a Comment