2013-07-12 51 views
1

我有一個ASP.Net MVC 4項目。如果我想TinyMCE的使用gzip的我需要使用以下在我的網頁(例如):TinyMCE ashx從IIS7中獲取404錯誤

<script type="text/javascript" src="/Scripts/tiny_mce/tiny_mce_gzip.js"></script> 
<script type="text/javascript"> 
    tinyMCE_GZ.init({ 
     plugins: 'style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras', 
     themes: 'simple,advanced', 
     languages: 'en', 
     disk_cache: true, 
     debug: false 
    }); 
</script> 

我注意到這部作品在使用開發Web服務器測試偉大的,但在展開時IIS7它沒有。進一步的調查顯示就這一請求的404(找不到文件):

/Scripts/tiny_mce/tiny_mce_gzip.ashx?js=true&diskcache=true&core=true&suffix=&themes=simple%2Cadvanced&plugins=style%2Clayer... 

的ashx的文件不存在相應的文件夾,但IIS不會成爲它的一些原因。我嘗試添加以下路由處理程序,但沒有什麼不同:

routes.IgnoreRoute("{resource}.ashx/{*pathInfo}"); 
routes.IgnoreRoute("{*allashx}", new { allashx = @".*\.ashx(/.*)?" }); 

回答

0

解決

已經搜遍互聯網,我看到有很多有同樣的問題,似乎沒有人發現了一個解! (即使在TinyMCE支持頁面上)。所以,我提出一個解決方案,我希望得到犯規:)倔強

您需要配置的唯一事情是在Global.asax「TinyMceScriptFolder」變量 - 它必須指向你的TinyMCE的腳本文件夾(杜)(使確保你不要一開始就有一個/否則該路徑的路由處理程序將拒絕它,它會從你的網站的根工作在任何情況下)

TinyMCEGzipHandler.cs(從原來的ashx的文件複製,但幾個增加)

using System; 
using System.Web; 
using System.IO; 
using System.IO.Compression; 
using System.Security.Cryptography; 
using System.Text; 
using System.Text.RegularExpressions; 
using Property; 

namespace Softwarehouse.TinyMCE 
{ 
    public class GzipHandler : IHttpHandler 
    { 
     private HttpResponse Response; 
     private HttpRequest Request; 
     private HttpServerUtility Server; 

     public void ProcessRequest(HttpContext context) 
     { 
      this.Response = context.Response; 
      this.Request = context.Request; 
      this.Server = context.Server; 
      this.StreamGzipContents(); 
     } 

     public bool IsReusable 
     { 
      get { return false; } 
     } 

     #region private 

     private void StreamGzipContents() 
     { 
      string cacheKey = "", cacheFile = "", content = "", enc, suffix, cachePath; 
      string[] plugins, languages, themes; 
      bool diskCache, supportsGzip, isJS, compress, core; 
      int i, x, expiresOffset; 
      GZipStream gzipStream; 
      Encoding encoding = Encoding.GetEncoding("windows-1252"); 
      byte[] buff; 

      // Get input 
      plugins = GetParam("plugins", "").Split(','); 
      languages = GetParam("languages", "").Split(','); 
      themes = GetParam("themes", "").Split(','); 
      diskCache = GetParam("diskcache", "") == "true"; 
      isJS = GetParam("js", "") == "true"; 
      compress = GetParam("compress", "true") == "true"; 
      core = GetParam("core", "true") == "true"; 
      suffix = GetParam("suffix", "") == "_src" ? "_src" : ""; 
      cachePath = Server.MapPath("/" + MvcApplication.TinyMceScriptFolder); // Cache path, this is where the .gz files will be stored 
      expiresOffset = 10; // Cache for 10 days in browser cache 

      // Custom extra javascripts to pack 
      string[] custom = 
       { 
/* 
      "some custom .js file", 
      "some custom .js file" 
     */ 
       }; 

      // Set response headers 
      Response.ContentType = "text/javascript"; 
      Response.Charset = "UTF-8"; 
      Response.Buffer = false; 

      // Setup cache 
      Response.Cache.SetExpires(DateTime.Now.AddDays(expiresOffset)); 
      Response.Cache.SetCacheability(HttpCacheability.Public); 
      Response.Cache.SetValidUntilExpires(false); 

      // Vary by all parameters and some headers 
      Response.Cache.VaryByHeaders["Accept-Encoding"] = true; 
      Response.Cache.VaryByParams["theme"] = true; 
      Response.Cache.VaryByParams["language"] = true; 
      Response.Cache.VaryByParams["plugins"] = true; 
      Response.Cache.VaryByParams["lang"] = true; 
      Response.Cache.VaryByParams["index"] = true; 

      // Is called directly then auto init with default settings 
      if (!isJS) 
      { 
       Response.WriteFile(Server.MapPath("/" + MvcApplication.TinyMceScriptFolder + "/tiny_mce_gzip.js")); 
       Response.Write("tinyMCE_GZ.init({});"); 
       return; 
      } 

      // Setup cache info 
      if (diskCache) 
      { 
       cacheKey = GetParam("plugins", "") + GetParam("languages", "") + GetParam("themes", ""); 

       for (i = 0; i < custom.Length; i++) 
        cacheKey += custom[i]; 

       cacheKey = MD5(cacheKey); 

       if (compress) 
        cacheFile = cachePath + "/tiny_mce_" + cacheKey + ".gz"; 
       else 
        cacheFile = cachePath + "/tiny_mce_" + cacheKey + ".js"; 
      } 

      // Check if it supports gzip 
      enc = Regex.Replace("" + Request.Headers["Accept-Encoding"], @"\s+", "").ToLower(); 
      supportsGzip = enc.IndexOf("gzip") != -1 || Request.Headers["---------------"] != null; 
      enc = enc.IndexOf("x-gzip") != -1 ? "x-gzip" : "gzip"; 

      // Use cached file disk cache 
      if (diskCache && supportsGzip && File.Exists(cacheFile)) 
      { 
       Response.AppendHeader("Content-Encoding", enc); 
       Response.WriteFile(cacheFile); 
       return; 
      } 

      // Add core 
      if (core) 
      { 
       content += GetFileContents("tiny_mce" + suffix + ".js"); 

       // Patch loading functions 
       content += "tinyMCE_GZ.start();"; 
      } 

      // Add core languages 
      for (x = 0; x < languages.Length; x++) 
       content += GetFileContents("langs/" + languages[x] + ".js"); 

      // Add themes 
      for (i = 0; i < themes.Length; i++) 
      { 
       content += GetFileContents("themes/" + themes[i] + "/editor_template" + suffix + ".js"); 

       for (x = 0; x < languages.Length; x++) 
        content += GetFileContents("themes/" + themes[i] + "/langs/" + languages[x] + ".js"); 
      } 

      // Add plugins 
      for (i = 0; i < plugins.Length; i++) 
      { 
       content += GetFileContents("plugins/" + plugins[i] + "/editor_plugin" + suffix + ".js"); 

       for (x = 0; x < languages.Length; x++) 
        content += GetFileContents("plugins/" + plugins[i] + "/langs/" + languages[x] + ".js"); 
      } 

      // Add custom files 
      for (i = 0; i < custom.Length; i++) 
       content += GetFileContents(custom[i]); 

      // Restore loading functions 
      if (core) 
       content += "tinyMCE_GZ.end();"; 

      // Generate GZIP'd content 
      if (supportsGzip) 
      { 
       if (compress) 
        Response.AppendHeader("Content-Encoding", enc); 

       if (diskCache && cacheKey != "") 
       { 
        // Gzip compress 
        if (compress) 
        { 
         using (Stream fileStream = File.Create(cacheFile)) 
         { 
          gzipStream = new GZipStream(fileStream, CompressionMode.Compress, true); 
          buff = encoding.GetBytes(content.ToCharArray()); 
          gzipStream.Write(buff, 0, buff.Length); 
          gzipStream.Close(); 
         } 
        } 
        else 
        { 
         using (StreamWriter sw = File.CreateText(cacheFile)) 
         { 
          sw.Write(content); 
         } 
        } 

        // Write to stream 
        Response.WriteFile(cacheFile); 
       } 
       else 
       { 
        gzipStream = new GZipStream(Response.OutputStream, CompressionMode.Compress, true); 
        buff = encoding.GetBytes(content.ToCharArray()); 
        gzipStream.Write(buff, 0, buff.Length); 
        gzipStream.Close(); 
       } 
      } 
      else 
       Response.Write(content); 
     } 

     private string GetParam(string name, string def) 
     { 
      string value = Request.QueryString[name] != null ? "" + Request.QueryString[name] : def; 

      return Regex.Replace(value, @"[^0-9a-zA-Z\\-_,]+", ""); 
     } 

     private string GetFileContents(string path) 
     { 
      try 
      { 
       string content; 

       path = Server.MapPath("/" + MvcApplication.TinyMceScriptFolder + "/" + path); 

       if (!File.Exists(path)) 
        return ""; 

       StreamReader sr = new StreamReader(path); 
       content = sr.ReadToEnd(); 
       sr.Close(); 

       return content; 
      } 
      catch (Exception ex) 
      { 
       // Ignore any errors 
      } 

      return ""; 
     } 

     private string MD5(string str) 
     { 
      MD5 md5 = new MD5CryptoServiceProvider(); 
      byte[] result = md5.ComputeHash(Encoding.ASCII.GetBytes(str)); 
      str = BitConverter.ToString(result); 

      return str.Replace("-", ""); 
     } 

     #endregion 
    } 
} 

的Global.asax

public const string TinyMceScriptFolder = "Scripts/htmleditor"; 

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute(TinyMceScriptFolder + "/tiny_mce_gzip.ashx"); 
} 

的Web.config

<system.webServer> 
    <httpHandlers> 
    <add name="TinyMCEGzip" verb="GET" path="tiny_mce_gzip.ashx" type="Softwarehouse.TinyMCE.GzipHandler"/> 
    </httpHandlers> 
</system.webServer>