This article describes the ASP.net program optimization js, CSS implementation of the merge and compression method. Share to everyone for your reference. The implementation methods are as follows:
When accessed, the JS and CSS are compressed and cached on the client,
Using the Yahoo.Yui.Compressor component to complete, users can click here to download the site.
Create a IHttpHandler to process files
Copy Code code as follows:
public class Combinefiles:ihttphandler
{
Private Const string Cachekeyformat = "_cachekey_{0}_";
Private Const BOOL Iscompress = TRUE; Need to compress
public bool IsReusable
{
Get
{
return false;
}
}
public void ProcessRequest (HttpContext context)
{
HttpRequest request = context. Request;
HttpResponse response = context. Response;
String CacheKey = String. Empty;
String type = Request. querystring["type"];
if (!string. IsNullOrEmpty (Type) && (type = = "CSS" | |-type = = "JS")
{
if (type = = "JS")
{
Response. ContentType = "Text/javascript";
}
else if (type = = "CSS")
{
Response. ContentType = "Text/css";
}
CacheKey = string. Format (Cachekeyformat, type);
Compresscacheitem CacheItem = Httpruntime.cache[cachekey] as Compresscacheitem;
if (CacheItem = null)
{
String content = String. Empty;
String path = context. Server.MapPath ("");
Find all of the JS or CSS files in this directory, of course, can also be configured, requirements request to compress which files
This is where all the files are requested to be compressed.
string[] files = directory.getfiles (path, "*." + type);
StringBuilder sb = new StringBuilder ();
foreach (string fileName in Files)
{
if (file.exists (fileName))
{
String readstr = File.readalltext (FileName, Encoding.UTF8);
Sb. Append (READSTR);
}
}
Content = sb. ToString ();
Start compressing files
if (iscompress)
{
if (type. Equals ("JS"))
{
Content = javascriptcompressor.compress (content);
}
else if (type. Equals ("CSS"))
{
Content = csscompressor.compress (content);
}
}
Input to the client can also be gzip compression, here is omitted
CacheItem = new Compresscacheitem () {type = type, content = content, Expires = DateTime.Now.AddDays (30)};
HttpRuntime.Cache.Insert (CacheKey, CacheItem, NULL, Cacheitem.expires, TimeSpan.Zero);
}
String ifmodifiedsince = Request. headers["If-modified-since"];
if (!string. IsNullOrEmpty (ifmodifiedsince)
&& timespan.fromticks (Cacheitem.expires.ticks-datetime.parse (ifmodifiedsince). Ticks). Seconds < 0)
{
Response. StatusCode = (int) System.Net.HttpStatusCode.NotModified;
Response. Statusdescription = "Not Modified";
}
Else
{
Response. Write (cacheitem.content);
Setclientcaching (response, cacheitem.expires);
}
}
}
private void setclientcaching (HttpResponse response, DateTime expires)
{
Response. Cache.setetag (DateTime.Now.Ticks.ToString ());
Response. Cache.setlastmodified (DateTime.Now);
Public to specify that the response can be cached by the client and the share (proxy) cache.
Response. Cache.setcacheability (Httpcacheability.public);
is the maximum absolute time that allows a document to exist before it is considered obsolete.
Response. Cache.setmaxage (Timespan.fromticks) (expires. Ticks));
Response. Cache.setslidingexpiration (TRUE);
}
Private Class Compresscacheitem
{
<summary>
Type JS or CSS
</summary>
public string Type {get; set;}//JS CSS
<summary>
Content
</summary>
public string Content {set;
<summary>
Expiration time
</summary>
Public DateTime Expires {set;
}
}
Finally configure the Combinefiles.axd file in the configuration file, the specific configuration slightly
References are as follows
Copy Code code as follows:
<script type= "Text/javascript" src= "/js/combinefiles.axd?type=js" ></script>
<link rel= "stylesheet" type= "Text/css" href= "/css/combinefiles.axd?type=css"/>
I hope this article will help you with the ASP.net program design.