壓縮

2012-01-19 40 views
1

的負載形式,我在寫我的一個基於客戶端的活動現場壓縮

public ActionResult Javascript(string filename) 
{ 
    var fileLocation = Server.MapPath(string.Format("~/Views/SiteFiles/{0}/js/{1}", Profile.SiteSlug, filename)); 
    if (!System.IO.File.Exists(fileLocation)) 
     fileLocation = Server.MapPath(string.Format("~/js/{0}", filename)); 

    return JavaScript(System.IO.File.ReadAllText(fileLocation)); 
} 

基本就產生JavaScript文件定製CMS解決方案的過程中,它會檢查當前網站文件夾以查看如果Javascript文件存在,如果它不存在,它會嘗試從根js文件夾加載它,而所有url看起來像文件位於同一個文件夾中

對文件的調用通常適用於第一次,有時甚至是多次,但是隨機地沒有改變任何東西,我開始從Firefox獲得消息 enter image description here Chrome會顯示此消息 enter image description here 當我嘗試從提琴手響應解碼,我得到這個消息 enter image description here

我才能真正困惑是怎麼回事,什麼我需要做什麼來解決這個問題

+0

似乎像編碼的一些問題..你在IIS中使用Http響應壓縮或使用一些HttpModule .. –

+0

不使用任何HttpModule,直接IIS –

+1

以及什麼是幻數? – shanabus

回答

0

我結束了在網站上禁用靜態和動態內容壓縮IIS和它開始工作,不知道爲什麼它會一直有時並沒有將其壓縮其他人,仍然會深入挖掘問題,當我得到時間

1

我並不確定你爲什麼會遇到編碼/解碼錯誤,但我曾在一個團隊中以不同的方式解決了類似的問題。

public ActionResult Index(string file, bool? compress) 
{ 
    List<string> requestedFiles; 
    if (file == "documentation") 
    { 
     var path = Server.MapPath("~/Views/Javascript"); 
     requestedFiles = Directory.GetFiles(path, "*.js").Select(x => Path.GetFileNameWithoutExtension(x)).ToList(); 
     return View(file, requestedFiles); 
    } 

    requestedFiles = file.Split(',').Select(x => x.Trim()).ToList(); 

    var javascript = new StringBuilder(); 
    foreach (var filePath in requestedFiles) 
    { 
     using (var fs = new FileStream(Path.Combine(Server.MapPath("~/Views/Javascript"), filePath + ".js"), FileMode.Open)) 
     { 
      var bytes = new byte[fs.Length]; 
      fs.Read(bytes, 0, bytes.Length); 
      javascript.AppendLine(Encoding.UTF8.GetString(bytes).Replace("~/", FullyQulifiedPathToRoot())); 
     } 
    } 

    var compressed = javascript.ToString(); 

    if (!compress.HasValue || compress == true) 
    { 
     compressed = JavaScriptCompressor.Compress(javascript.ToString(), true, true, true, false, int.MaxValue, Encoding.UTF8, CultureInfo.CurrentCulture, false); 
    } 

    return JavaScript(compressed); 
} 

private string FullyQulifiedPathToRoot() 
{ 
    return System.Web.HttpContext.Current.Request.Url.Scheme + "://" + System.Web.HttpContext.Current.Request.Url.Host + VirtualPathUtility.ToAbsolute("~/"); 
} 

這確實需要使用的Yahoo.Yui.Compressor dll

+0

謝謝,我會檢查出來,當我今天晚上回家 –

+0

嗯...沒有什麼不同。我可能嘗試連接YUI壓縮機,看看是否不能解決我的壓縮問題......這是令人沮喪的 –