9

I'm trying to enable GZip compress for SVG in an Azure Web Site using web.config transforms without success. Here is what my transform looks like:

<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>

This should both add the mime type for SVG, which Azure doesn't seem to have, and then enable compression. I've verified the mime type addition works fine, but upon publishing I get an error for the compression elements:

No element in the source document matches '/configuration/system.webServer/httpCompression/staticTypes'

Removing the compression from the transform and adding it directly to my web.config file removes the error, but I still don't see the compression in the HTTP headers. Here are the response headers:

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
4

4 回答 4

5

以下是如何在 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 行之前捕获所有内容。

于 2014-08-05T16:34:49.047 回答
1

不幸的是,不能在Azure 网站上对image/xml+svgmime 类型使用内置的 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>
于 2014-06-01T20:50:51.123 回答
0

我有 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)。

于 2014-05-02T14:19:37.063 回答
0

上述解决方案对我有用,但我首先必须删除文件扩展名。之后,我得到了我想要的结果。

<staticContent>        
    <remove fileExtension=".svg" />
    <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
</staticContent>
于 2017-07-21T18:35:52.283 回答