10

I'm serving .JSON files, but even though the file exist, IIS keeps throwing 404 error when any of the file is accessed. I tried renaming one of the file to .JS, and it works.

Any pointer what setting can cause this issue?

Adrian Godong
  • 577
  • 4
  • 9
  • 20

6 Answers6

22

By default, IIS in W2K3 and above won't serve files that aren't of a MIME type that it knows about (instead returning 404 errors).

You need to add a MIME type to IIS to allow it to serve that type of file. You can set it at the site level or at the server level.

To set this for the entire server:

  • Open the properties for the server in IIS Manager and click MIME Types
  • Click "New". Enter "JSON" for the extension and "application/json" for the MIME type.
Evan Anderson
  • 141,071
  • 19
  • 191
  • 328
  • I add a new MIME type, restart the web site, but still 404. Thanks anyway, I think this may be one of the issue. – Adrian Godong Jul 13 '09 at 17:24
  • Hmm. Double-check what you've changed. The "renamed the file and it worked" symptom is the *classic* indicator of this behaviour. – Evan Anderson Jul 13 '09 at 17:45
  • Triple checked it. I'm assuming that you don't need to restart the whole server, right? – Adrian Godong Jul 13 '09 at 17:46
  • Hmm... I'm stuggling to tell you what to do, then. I just did it myself in a clean VM of Windows Server 2003 SP2. I installed IIS, created a "foo.json" file in "C:\inetpub\wwwroot", verified that I got a 404 when I hit "http://localhost/foo.json" in a browser, added the MIME type for ".JSON"/"application/json" to the site, then went over to my still-open IE window and hit F5 and saw the page load. I think you've got something else in play, too. – Evan Anderson Jul 13 '09 at 18:02
  • Yes, that's what I'm worried about. – Adrian Godong Jul 13 '09 at 18:08
  • Do you have any ISAPI filters (rewrite, etc?) in play? To make sure it's not some other setting on your site's configuration mucking this up consider creating a temporary test site on a non-standard port or unused IP address and configuring it there. – Evan Anderson Jul 13 '09 at 18:41
  • +1, I ran into this exact issue today and luckily I saw this answer last night. – ThatGraemeGuy Jul 14 '09 at 09:08
  • 1
    I also needed to make sure Web Service Extensions were enabled to make this work for me... – kind_robot Nov 14 '11 at 20:26
  • @WarrenP - I believe they're considering a "secure by default"-type setting. (Not saying I agree or disagree w/ the idea, but I believe that's the justification.) – Evan Anderson Jan 17 '16 at 01:43
  • @WarrenP Like I said-- I'm not agreeing or disagreeing with MSFT's design choice. As stated in other answers here, IIS will allow you to create a wildcard mapping so, in your example, it isn't necessary to create large numbers of mappings if you can live w/ a wildcard. Just add a "*" mapping at the top of a website and you, in effect, nullify the design decision. – Evan Anderson Jan 21 '16 at 14:01
  • Perfect. I think I missed that fact earlier. Thanks. Deleted my comments as they are unprofessional. – Warren P Jan 21 '16 at 15:37
3

I had the same problem. IIS does something called MIME type filtering. If it dosn't know a specific file extension's MIME type, it returns a 404 error.

On IIS <7: Open the site (or server) properties. Click on the HTTP-Header tab. Click on the MIME Types button. Add the file type * with the MIME type "application/octet-stream".

For IIS 7: Open IIS manager. Click the server or website. Double-click the MIME Types feature icon. In the Actions pane, click Add. Populate the "File Name Extension" box with * and the MIME Type box with "application/octet-stream".

Eric Falsken
  • 627
  • 2
  • 8
  • 21
2

I added MIME type .json - text/json to the site in IIS to view in browser as text.

neildeadman
  • 664
  • 2
  • 20
  • 33
1

To consolidate answers into one more general answer:

Here comes the caveat:

  • Unless a script handler (active content engine, eg, ASP/ASPX/PHP/Java/ISAPI/etc) is associated with them
    • this means that IIS treats them as active content, not static files
  • Active content files served by any extension must have that extension allowed in the Web Service Extensions restriction list (aka ISAPI/CGI restrictions in IIS 7)
    • if you're getting a 404 on an active content type in IIS 7, it may also mean your handler is not installed - so you need to actually go install the component that supports that file extension (for example, if .aspx files generate a 404, you probably don't have ASP.Net installed)

If you're experiencing this sort of problem, open up the website's web log files, and have a look at the sub-status code (i.e. 404 3) to determine exactly why your website isn't serving content for IIS 6, or

  • use Failed Request Tracing
  • (or just the detailed errors on the local console)

in IIS 7.

TristanK
  • 8,953
  • 2
  • 27
  • 39
  • This "caveat" doesn't make sense: "...this means that IIS treats them as active content, not static files" Web.config declares json files as static: –  Feb 16 '14 at 12:47
  • "IIS won't serve static files that don't have a MIME type associated with them unless a script handler is associated with them" still works as (IMO) a true statement. In the case of someone unable to serve them, it sounds like theirs doesn't have this definition in their config files anywhere. Perhaps your copy of Windows is more recent than theirs? – TristanK Apr 30 '14 at 07:40
1

I had this problem too - and the problem turned out to be file permissions on the folder where the json file was stored. I answered a similar question here.

These folks are all correct to say you need to add the MIME type.

The MIME type enables the file extension on the server and the Mapping Handler tells IIS what to do with it.

So you also have to add the Mapping Handler, and for that you have to have the Windows Feature called Classic Asp installed in order to handle the mapping.

Here is how to add the Mapping Handler:

  1. Open IIS7 Manager
  2. Go to the properties for IIS Server (or just the site, if you don't want a global setting)
  3. Click on Handler Mappings
  4. Click on Add a Script Map
  5. Enter the properties:
    • Request Path: *.json
    • Executable: C:\WINDOWS\system32\inetsrv\asp.dll
    • Name: JSON (you can call it anything you like)
  6. Click OK, and you are done. It should start working and you don't need an IIS reset.

Also, some folks will tell you to use `application/x-javascript' as the mime-type application, but JSON is not javascript, per se (it is a subset, however) and so it should be 'application/json' instead, as it is that IANA-registered media type for JSON.

bgmCoder
  • 706
  • 4
  • 16
  • 29
0

Also check Request Filtering in IIS. If ".json" is there and blocked, nothing else will work.

enter image description here

Glen Little
  • 435
  • 2
  • 7
  • 17