在Azure网站中为SVG启用GZip压缩?

我正在尝试在Azure网站中使用web.config转换为SVG启用GZip压缩而没有成功。 这是我的变换看起来像:

<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
  <system.webServer>
    <httpCompression>
      <staticTypes>
        <add mimeType="image/svg+xml" enabled="true" xdt:Transform="Insert" />
      </staticTypes>
    </httpCompression>
    <staticContent xdt:Transform="Insert">
      <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
    </staticContent>
  </system.webServer>
</configuration>

这应该为Azure似乎没有的SVG添加mime类型,然后启用压缩。 我已经验证了mime类型的加法工作正常,但在发布后,我得到了压缩元素的错误:

源文档中没有元素与'/configuration/system.webServer/httpCompression/staticTypes'匹配

从转换中删除压缩并直接将其添加到我的web.config文件中会消除该错误,但我仍然看不到HTTP头中的压缩。 以下是响应标题:

Accept-Ranges:bytes
Content-Length:23265
Content-Type:image/svg+xml
Date:Mon, 10 Jun 2013 17:19:37 GMT
ETag:"c4e9ec93d765ce1:0"
Last-Modified:Mon, 10 Jun 2013 12:39:41 GMT
Server:Microsoft-IIS/8.0
X-Powered-By:ASP.NET
X-Powered-By:ARR/2.5
X-Powered-By:ASP.NET

这里是你如何在你的web.config中启用它:

<configuration>
   <system.webServer>
      <staticContent>
         <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
      </staticContent>
      <httpCompression>
         <staticTypes>
           <remove mimeType="*/*" />
           <add mimeType="image/svg+xml" enabled="true" />
           <add mimeType="*/*" enabled="false" />
         </staticTypes>
      </httpCompression>
   </system.webServer>
</configuration>

关键是删除全部(后来重新添加)。 如果你没有,那么svg行基本上被忽略,因为catch-all从applicationhost.config继承,并在达到svg行之前捕获所有行。


不幸的是,在Azure网站上为image/xml+svg mime类型使用内置http压缩是不可能的。 如果您使用的是Azure Web Roles, 必须更改一些IIS设置才能做到这一点

我不想经历那种麻烦,所以我只是在MVC中创建了一个控制器来处理.svg文件。

[AttributeRouting.RoutePrefix("static")]
public class ContentController : Controller
{
    [GET(@"fonts/{fileName:regex(^[w-.]+.svg$)}")]
    [Compress, OutputCache(
        Duration = 3600 * 24 * 30,
        Location = OutputCacheLocation.Any,
        VaryByContentEncoding = "gzip;deflate",
        VaryByParam = "fileName")]
    public ActionResult SvgFont(string fileName)
    {
        var path = Server.MapPath("~/Content/fonts/" + fileName);
        if (!System.IO.File.Exists(path)) return HttpNotFound();
        return File(path, "image/svg+xml");
    }
}

public class CompressAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.HttpContext.CompressResult();
    }
}

public static class HttpContextExtensions
{
    public static bool CompressResult(this HttpContextBase context)
    {
        var request = context.Request;
        var response = context.Response;
        if (request == null || response == null) return false;
        var filter = response.Filter;
        if (filter is GZipStream || filter is DeflateStream) return false;
        var acceptEncoding = (request.Headers["Accept-Encoding"] ?? string.Empty).ToLowerInvariant();
        if (acceptEncoding.Contains("gzip"))
        {
            response.Filter = new GZipStream(filter, CompressionMode.Compress);
            response.AddHeader("Content-Encoding", "gzip");
            response.AppendHeader("Vary", "Content-Encoding");
            return true;
        }
        if (acceptEncoding.Contains("deflate"))
        {
            response.Filter = new DeflateStream(filter, CompressionMode.Compress);
            response.AddHeader("Content-Encoding", "deflate");
            response.AppendHeader("Vary", "Content-Encoding");
            return true;
        }
        return false;
    }
}

您还需要将其添加到您的Web.config文件中,以便MVC将处理扩展名为.svg的路由

<system.webServer>
  <handlers>
    <add name="StaticMvcHandler" path="static/fonts/*.svg" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
  </handlers>
</system.webServer>

我有一个Azure网站的下列配置条目:

    <system.webServer>
       <urlCompression doStaticCompression="true" doDynamicCompression="true" />
    </system.webServer>

  <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
  <!-- Scalable Vector Graphics iPhone, iPad -->
  <mimeMap fileExtension=".svgz" mimeType="image/svg+xml" />

我也添加了.svgz扩展(对于压缩的svg)。

链接地址: http://www.djcxy.com/p/62717.html

上一篇: Enable GZip compression for SVG in Azure Web Sites?

下一篇: What is the difference between an Azure Web Site and an Azure Web Role