14

Let's say I have a simple web page with svg image in it:

<img src="foobar.svg" alt="not working" />

If I make this page as static html page and view it directly svg is displayed. If I type the address of this svg -- it is displayed.

But when I make this as .aspx page and launch it dynamically from Visual Studio I get alt text. If I type the address of this svg (from localhost, not as a local file) -- browser tries to download it instead of displaying.

I already defined mime type in IIS (for entire server -- "image/svg+xml") and restarted IIS. Same effect as before.

Question: what should I do more?

Update

WireShark won't work (it is in documentation), I tried also RawCap, but it cannot trace my connection (odd), luckily Fiddler worked:

From client:

GET http://127.0.0.1:1731/svg/document_edit.svg HTTP/1.1
Host: 127.0.0.1:1731
User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:10.0.1) Gecko/20100101 Firefox/10.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive

Answer from server:

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Thu, 16 Feb 2012 11:14:38 GMT
X-AspNet-Version: 4.0.30319
Cache-Control: private
Content-Type: application/octet-stream
Content-Length: 87924
Connection: Close

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
   xmlns:

*** FIDDLER: RawDisplay truncated at 128 characters. Right-click to disable truncation. ***

For the record, here is useful Q&A for Fiddler: https://stackoverflow.com/questions/826134/how-to-display-localhost-traffic-in-fiddler-while-debugging-an-asp-net-applicati

greenoldman
  • 313
  • 2
  • 5
  • 14
  • Think you might get better answers for this on SO. In short, assuming ASPX is already working, you'd need to emit SVG as the MIME type from the ASPX page in order for the browser to believe it's an SVG file. You are, presumably, trying to dynamically emit SVG from the page? If so, AFAIK if it works as a single page, it'll work in an IMG entry. – TristanK Feb 14 '12 at 11:29
  • @TristanK, aspx is not emitting "SVG as the MIME type", it simply contains ` – greenoldman Feb 14 '12 at 11:46
  • I still don't understand what you're describing, but I think this is a development question, so would be better off asked on StackOverflow. – TristanK Feb 14 '12 at 21:38
  • 2
    Grab a copy of [Fiddler](http://fiddler2.com/fiddler2/) then trace the two different requests. That might give you some clues as to why a direct request for the image is behaving differently in each of these cases. The clues will most likely be in the headers returned. Without this data all we can do is but guess at what responses your server is returning. – Kev Feb 15 '12 at 01:45
  • @macias As Kev said, you're going to need to trace the traffic. Fiddler or plain old Wireshark should be able to pick it up. – Chris S Feb 15 '12 at 02:31
  • @Kev, when you access local file (directly), there is no traffic, so I cannot trace it. For localhost, I updated the question. – greenoldman Feb 16 '12 at 11:21

6 Answers6

14

From your Fiddler trace it appears that you're serving your pages using the built-in Visual Studio web server:

Server: ASP.NET Development Server/10.0.0.0

If this was being served by IIS7 then we'd see:

Server: Microsoft-IIS/7.5

The built-in Visual Studio web server only has a limited set of mime-types it can serve and has no knowledge of mime types you set for IIS7. I wrote up an answer to a similar problem on Stack Overflow a while back:

Setting MIME types using the ASP.NET Development Server

The built-in server is serving your .svg file as:

Content-Type: application/octet-stream

This is probably what's causing the browser to prompt to download.

In Visual Studio check that you're using IIS Express by opening your site's project properties and selecting the "Web" tab from the vertical tab list:

enter image description here

If you don't have IIS 7.5 Express installed you can get it from here:

http://www.microsoft.com/download/en/details.aspx?id=1038

You will need Visual Studio 2010 Service Pack 1 to take full advantage:

http://support.microsoft.com/kb/983509

IIS Express support

Visual Studio 2010 SP1 enables you to use the Internet Information Services (IIS) 7.5 Express as the local hosting server for the website and Web Application Projects.

Note IIS 7.5 Express is not included in SP1, and you must download it separately. For more information, visit the following blog: http://weblogs.asp.net/scottgu/archive/2011/01/03/vs-2010-sp1-beta-and-iis-developer-express.aspx

When you've done that you can add the .svg mime type to your application's web.config file:

<configuration>
   <system.webServer>
      <staticContent>
         <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
      </staticContent>
   </system.webServer>
</configuration>
Kev
  • 7,777
  • 17
  • 78
  • 108
  • Many thanks. In my case it went somewhat differently -- to switch to IIS (I have ISS, not ISSExpress) I had to run VS in admin mode, after that it was all -- I didn't define svg in web.config because I defined it already in IIS. – greenoldman Feb 16 '12 at 13:39
2

As mentioned above, Cassini ignores these settings in web.config so one has to use IIS Express instead (at VS project settings) https://stackoverflow.com/questions/5924647/setting-mime-types-using-the-asp-net-development-server

To get more info on how to configure MIME types using the admin UI or using web.config for IIS or IIS Express see: http://4rapiddev.com/tips-and-tricks/add-mime-type-flv-mp4-in-iis-for-a-website-or-global/ and http://4rapiddev.com/tips-and-tricks/add-mime-type-flv-mp4-to-web-config-in-iis-7/

  • Are you asking this as a new question, or rhetorically? If the former, please [ask a new question](http://serverfault.com/questions/ask) with full details. If the latter, this should probably be a comment - as it stands it's a very poor answer ([see here for why](http://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers)) – voretaq7 Jun 12 '12 at 16:21
  • revised with better links – George Birbilis Jun 12 '12 at 16:34
  • much better :-) – voretaq7 Jun 12 '12 at 18:14
2

I've used Kev answer, by:

  1. Installing IIS 8.0 Express from Web Platform Installer
  2. Changing project's properties to use IIS Express and creating Virtual Directory for it
  3. Adding in web.config's configuration → system.webServer
<staticContent>
    <remove fileExtension=".svg" />
    <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
</staticContent>
2

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

1

I am running IIS7, and was able to fix this by right-clicking on the server in IIS and choosing properties. Then I clicked on the MIME Types... button. I then clicked New. For the extension I typed in .svg. For the MIME Type I typed in image/svg+xml. I then saved is all and did an iisreset from the command prompt. Worked great.

Troy Tabor
  • 11
  • 1
0

If you have access to IIS then this also fixes the problem: http://httpjunkie.com/2014/884/svg-no-iis/

Rahatur
  • 145
  • 7