茄子在线看片免费人成视频,午夜福利精品a在线观看,国产高清自产拍在线观看,久久综合久久狠狠综合

    <s id="ddbnn"></s>
  • <sub id="ddbnn"><ol id="ddbnn"></ol></sub>

  • <legend id="ddbnn"></legend><s id="ddbnn"></s>

    Asp.net程序優(yōu)化js、css實(shí)現(xiàn)合并與壓縮的方法
    來(lái)源:易賢網(wǎng) 閱讀:1311 次 日期:2014-12-26 15:47:37
    溫馨提示:易賢網(wǎng)小編為您整理了“Asp.net程序優(yōu)化js、css實(shí)現(xiàn)合并與壓縮的方法”,方便廣大網(wǎng)友查閱!

    本文實(shí)例講述了Asp.net程序優(yōu)化js、css實(shí)現(xiàn)合并與壓縮的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

    訪問(wèn)時(shí)將js和css壓縮并且緩存在客戶端,

    采用的是Yahoo.Yui.Compressor組件來(lái)完成的,用戶可以點(diǎn)擊此處本站下載。

    創(chuàng)建一個(gè)IHttpHandler來(lái)處理文件

    代碼如下:

    public class CombineFiles : IHttpHandler

    {

    private const string CacheKeyFormat = "_CacheKey_{0}_";

    private const bool IsCompress = true; //需要壓縮

    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("");

    //找到這個(gè)目錄下所有的js或css文件,當(dāng)然也可以進(jìn)行配置,需求請(qǐng)求壓縮哪些文件

    //這里就將所的有文件都請(qǐng)求壓縮

    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();

    // 開(kāi)始?jí)嚎s文件

    if (IsCompress)

    {

    if (type.Equals("js"))

    {

    content = JavaScriptCompressor.Compress(content);

    }

    else if (type.Equals("css"))

    {

    content = CssCompressor.Compress(content);

    }

    }

    //輸入到客戶端還可以進(jìn)行Gzip壓縮 ,這里就省略了

    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 以指定響應(yīng)能由客戶端和共享(代理)緩存進(jìn)行緩存。

    response.Cache.SetCacheability(HttpCacheability.Public);

    //是允許文檔在被視為陳舊之前存在的最長(zhǎng)絕對(duì)時(shí)間。

    response.Cache.SetMaxAge(TimeSpan.FromTicks(expires.Ticks));

    response.Cache.SetSlidingExpiration(true);

    }

    private class CompressCacheItem

    {

    /// <summary>

    /// 類(lèi)型 js 或 css

    /// </summary>

    public string Type { get; set; } // js css

    /// <summary>

    /// 內(nèi)容

    /// </summary>

    public string Content { set; get; }

    /// <summary>

    /// 過(guò)期時(shí)間

    /// </summary>

    public DateTime Expires { set; get; }

    }

    }

    最后在配置文件中配置一下CombineFiles.axd文件,具體配置略

    引用如下

    復(fù)制代碼 代碼如下:<script type="text/javascript" src="/js/CombineFiles.axd?type=js"></script>

    <link rel="stylesheet" type="text/css" href="/css/CombineFiles.axd?type=css" />

    希望本文所述對(duì)大家的asp.net程序設(shè)計(jì)有所幫助。

    更多信息請(qǐng)查看IT技術(shù)專(zhuān)欄

    更多信息請(qǐng)查看網(wǎng)絡(luò)編程
    由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門(mén)公布的正式信息和咨詢?yōu)闇?zhǔn)!

    2026上岸·考公考編培訓(xùn)報(bào)班

    • 報(bào)班類(lèi)型
    • 姓名
    • 手機(jī)號(hào)
    • 驗(yàn)證碼
    關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡(jiǎn)要咨詢 | 簡(jiǎn)要咨詢須知 | 新媒體/短視頻平臺(tái) | 手機(jī)站點(diǎn) | 投訴建議
    工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
    云南網(wǎng)警備案專(zhuān)用圖標(biāo)
    聯(lián)系電話:0871-65099533/13759567129 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
    咨詢QQ:1093837350(9:00—18:00)版權(quán)所有:易賢網(wǎng)
    云南網(wǎng)警報(bào)警專(zhuān)用圖標(biāo)