首页 文章

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

提问于
浏览
8

我正在尝试使用web.config转换在Azure网站中为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 回答

  • 0

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

  • 4

    遗憾的是,无法在Azure网站上使用内置的http压缩来实现 image/xml+svg mime类型 . 如果您使用的是Azure Web角色,则必须更改某些IIS设置才能执行 is .

    我不想经历那么麻烦,所以我只是在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>
    
  • 1

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

  • 0

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

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

相关问题