SandBag_1996
SandBag_1996

Reputation: 1601

Why does IIS require mime-types to be declared?

In one of my assignments I was trying to render an SVG image. It took me some time to come across one link which states that, in order for IIS express to render an SVG image, you have to include the following code in your web.config file

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

It worked. But I don't understand why/how? Previously I thought that the server should send the correct Content-Type header. But my svg code was in Javascript. I thought (maybe wrongly) that IIS sends only HTML files to the client side, and those HTML files have the Javascript linked in the header. So, does that mean that IIS is scanning all the javascript files associated with the HTML too? That seems a little hard to believe. If I have 10 javascript files in my HTML main file, and one of them has an SVG file in it, does that mean IIS will scan all of them, and then find out SVG is missing? Is this how it works?

Thanks

Upvotes: 5

Views: 2990

Answers (1)

McGarnagle
McGarnagle

Reputation: 102743

When your browser hits a URL, it initially only downloads the HTML. For every linked file (Javascript, images, CSS, SVG, etc), the browser will make a separate request to the server. And as you noted, IIS won't serve those files unless it recognizes the MIME type.

To answer your question, no, that's not how it works. IIS doesn't scan the HTML files, it just responds passively to requests from the client (browser). It's the browser that parses the HTML and Javascript, and executes the Javscript, making an additional round-trips back to the server for linked resources as needed.

Edit

The purpose of MIME types for IIS is twofold:

  1. Restrict access to the server's resources. If the client request a Web.config file, then of course IIS should block that request, because the file is likely to contain sensitive information like passwords.
  2. Keep track of how to process each file type. For example, HTML files should generally just be sent, but ASPX files need to be processed first by ASP.Net, and then sent.

Upvotes: 7

Related Questions