在Azure Web Sites中启用SVG的GZip压缩?

9

我试图使用web.config变换为Azure Web站点中的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>

这应该同时添加SVG的MIME类型(Azure似乎没有),然后启用压缩。我已经验证了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
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行,因为“捕捉全部”从applicationhost.config继承,并在到达svg行之前捕捉所有内容。

1
我无法使用上述方法压缩我的SVG文件?还有其他需要注意的事项吗? - Sandip Bantawa

1
很遗憾,Azure Websites无法使用内置的http压缩来处理image/xml+svg mime类型。你需要更改一些IIS设置来实现这一点,但如果你使用Azure Web Roles则可以实现。我不想那么麻烦,所以我只是在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 网站上(而不是 WebRole)唯一有效的方法。 - Sha

0

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

<staticContent>        
    <remove fileExtension=".svg" />
    <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
</staticContent>

0

我有以下配置条目用于 Azure Web-Site:

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接