无法在 Visual Studio 2012 (IIS Express) 中显示 .svg 文件

Cannot display .svg files in Visual Studio 2012 (IIS Express)

我正在尝试使用 Visual Studio 2012IIS 在我的 Web 应用程序中显示 .svg 文件Express v8.0ASP .NET Web Forms.

我已经尝试过的事情:

  1. 将 .svg 扩展名添加到 web.config
<staticContent>
  <remove fileExtension=".svg" />
  <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
</staticContent>
  1. 正在为 C:\Users\UserName\Documents\IISExpress\config\applicationhost.config
  2. 添加 .svg 扩展名
<staticContent lockAttributes="isDocFooterFileName">
  ...
  <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
  ...
  1. 将图片URL复制到浏览器,显示正常

4.在IIS下发布站点,显示正常。此外,我们有一个开发人员使用 Visual Studio 2013,并且使用 IIS Express v8.5 显示正常。

我将 .svg 添加为图标,使用带有 class 的 span 元素作为背景 url 的文件,所以我不能使用这个解决方案:SVG files in VS2012

这是 class 添加到 span 的样式:

background: transparent url(images/svg/reports.svg) no-repeat scroll 0px 0px;

发生了什么事?

基于 con @user1429080 的建议,有一个解决方法(虽然不是最干净的工作方式):

My workaround for this was to create my own httphandler locally which overwrote the content-type for svg.

public class SvgHandler : IHttpHandler
{

    public bool IsReusable
    {
        get { return false; }
    }

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "image/svg+xml";
        context.Response.BinaryWrite(File.ReadAllBytes(context.Request.PhysicalPath));
        context.Response.End();
    }
}

and in web.config i added:

<httpHandlers>
  <add verb="*" path="*.svg" type="SvgHandler" />
</httpHandlers>

with this solution you don't have to use IIS express, you can just use the regular development server in visual studio 2010

来源:Visual Studio Not Displaying SVG image as background