129

How can I enable the download of *.json files from an old ASP.NET site (IIS6 I am led to believe)?

I am getting a 404 page instead of the JSON file.

Do I need to create a web.config file? What goes in it?

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
Lea Hayes
  • 62,536
  • 16
  • 62
  • 111
  • 1
    You probably need to [add a MIME type](http://www.microsoft.com/technet/prodtechnol/windowsserver2003/library/iis/cd72c0dc-c5b8-42e4-96c2-b3c656f99ead.mspx) for it. – vcsjones Nov 16 '11 at 20:35
  • 1
    For use in a javascript or to save on a hd? – Remy Nov 16 '11 at 20:36
  • I want to access it via jQuery.ajax(url:... – Lea Hayes Nov 16 '11 at 20:40
  • 2
    Adding that after the local IIS has a listing, on your next deploy it may throw a 500 server error from a duplicate listing so then comment it out. – Tom Mallard Jul 30 '12 at 18:54

6 Answers6

218

If you want to manually add support to your site, you can just add the following to your web.config in the system.webServer section:

<staticContent>
    <mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>

This will add a "local" configuration under IIS. This does not work in IIS6, but does work in IIS7 and newer.

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
ProVega
  • 5,864
  • 2
  • 36
  • 34
  • 6
    Updated your answer to include full `web.config` path. – GFoley83 Apr 06 '13 at 02:43
  • 1
    For some reason, this messed up my whole site - css no longer worked! – TheJeff Oct 09 '15 at 22:54
  • @TheJeff -- that happened to me, but it's because I added two staticContent mimeMap's, one for JSON (necessary) and one for CSV (redundant, I assumed it was necessary, but the IISExpress applicationConfig already had CSV). Therefore I got this error anytime I tried to access any staticfile: `Cannot add duplicate collection entry of type 'mimeMap' with unique key attribute 'fileExtension' set to '.csv' ` Once I removed the duplicate, no problem. – Nate Anderson Dec 23 '15 at 19:58
  • This solution does not work on IIS6, which was specifically requested. – Jon Adams Mar 17 '16 at 18:58
  • 2
    This will fix the problem when running locally, but pay attention when pushing live. If the mimeMap is already configured globally in IIS, you will get 500 errors. Simply remove this piece from your web.config in that case. – Evan Aug 24 '16 at 16:09
  • stopped loading .css files when I added that – Mahmoud Hboubati Apr 27 '17 at 17:28
  • Do you think any security issues if we open .json file like this? – Firnas Jan 11 '19 at 11:05
122

Add the JSON MIME type to IIS 6. Follow the directions at MSDN's Configure MIME Types (IIS 6.0).

  • Extension: .json
  • MIME type: application/json

Don't forget to restart IIS after the change.

UPDATE: There are easy ways to do this on IIS7 and newer. The op specifically asked for IIS6 help so I'm leaving this answer as-is. But this answer is still getting a lot of traffic even though IIS6 is very old now. Hopefully you're using something newer, so I wanted to mention that if you have a newer IIS7 or newer version see @ProVega's answer below for a simpler solution for those newer versions.

Jon Adams
  • 24,464
  • 18
  • 82
  • 120
  • 1
    Cheers but this is on a shared hosting package without access to anything other than web.config or global code file – Lea Hayes Nov 16 '11 at 21:03
  • If you don't have access to IIS 6.0 settings, and your provider's support won't add the MIME type for you, then you will have to switch providers, or to newer servers with IIS 7.0 on that provider if they offer it, to support this. – Jon Adams Nov 16 '11 at 21:05
  • Is it possible to create an aspx handler which simply opens file sets mime and return (like with php)? This would be a temporary workaround whilst waiting for change to Linux hosting – Lea Hayes Nov 16 '11 at 21:08
  • 1
    Yes, ASP.Net can pass the file through via an ASPX handler or even a simple page, but you won't be able to use the .json file type in the URL since it won't pass that request to the ASP.Net runtime. It will have to be something that passes through the ASPX runtime. If you don't have access to IIS settings, it will have to be one of the existing extensions like `.ASPX`. Again, switching to a server with IIS7 offers many more and easier solutions. – Jon Adams Nov 16 '11 at 21:11
  • To get it to work on IIS (assuming you have configuration access), you may need to also add a Handler Mapping for JSON (go to `IIS > Handler Mappings`, and then: Add a script map Request path: *.json Executable: C:\WINDOWS\system32\inetsrv\asp.dll Name: JSON – Matty J Jul 11 '13 at 10:46
  • @MattyJ: To be clear, you only need the ASP handler to process the .json request if you want ASP to generate the response. If it is an existing .json file, as the Op asked, adding the handler isn't necessary. – Jon Adams Jul 11 '13 at 12:57
  • @JonAdams That's why I added it as a comment and not an answer / edit to an answer. – Matty J Jul 12 '13 at 04:16
  • 1
    Restarting the app pool should be sufficient (it was in my case), rather than restarting IIS altogether. – kltft Jan 27 '20 at 18:41
  • 1
    @kltft True, an apppool restart will trigger the update, but only for that pool of course. These steps include changing the setting for everyone, and having them in an inconsistent level of settings would easily lead to confusion later. I recommend restarting the whole iis process for safety and sanity. But in a pinch, if everyone agrees to the pros/cons, an apppool restart could work. – Jon Adams Jan 27 '20 at 19:46
20

Solution is you need to add json file extension type in MIME Types

Method 1

Go to IIS, Select your application and Find MIME Types

enter image description here

Click on Add from Right panel

File Name Extension = .json

MIME Type = application/json

enter image description here

After adding .json file type in MIME Types, Restart IIS and try to access json file


Method 2

Go to web.config of that application and add this lines in it

 <system.webServer>
   <staticContent>
     <mimeMap fileExtension=".json" mimeType="application/json" />
   </staticContent>
 </system.webServer>
Community
  • 1
  • 1
Kaushal Khamar
  • 2,097
  • 1
  • 17
  • 23
17

When adding support for mimetype (as suggested by @ProVega) then it is also best practice to remove the type before adding it - this is to prevent unexpected errors when deploying to servers where support for the type already exists, for example:

<staticContent>
    <remove fileExtension=".json" />
    <mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
Mark Cooper
  • 6,738
  • 5
  • 54
  • 92
  • When you add only the mimeMap, sometimes it remove your css styles, so, the correct way is remove it first, thanks a lot – sgrysoft Dec 09 '18 at 02:42
  • Path to config file in IIS7 `C:\Windows\System32\inetsrv\config` file: **applicationHost.config** – O.O Feb 13 '19 at 11:24
5

Just had this issue but had to find the config for IIS Express so I could add the mime types. For me, it was located at C:\Users\<username>\Documents\IISExpress\config\applicationhost.config and I was able to add in the correct "mime map" there.

longda
  • 10,153
  • 7
  • 46
  • 66
  • This may be why it's not working for me through IIS Express. Unfortunately this isn't something I would want to have to configure on every dev machine so looks like I'll recommend sticking with .js extension for json config files. – jpierson Nov 07 '14 at 15:52
  • You can also do it in Web.Config - Look at this answer http://stackoverflow.com/a/19517275/643761 – Simcha Khabinsky Nov 09 '14 at 03:32
  • @SimchaKhabinsky For me the web.config wasn't enough. – Jean-Bernard Pellerin Jun 20 '15 at 02:19
  • 3
    If the web.config isn't enough, you may need to add this line before the `` node: ``. You shouldn't have to touch the applicationhost.config (or machine.config). That being said, modifying those files may be useful if you frequently create new sites and don't want to have to modify the web.config for each site. That may work for dev environments, but frequently you aren't able to touch the machine.config in production environments, so I still would recommend making the change in the web.config. – gilly3 Jun 21 '15 at 09:10
  • 1
    Thanks @gilly3 - the `remove` was the key for me – harriyott Oct 03 '15 at 07:21
0
  1. Navigate to C:\Users\username\Documents\IISExpress\config
  2. Open applicationhost.config with Visual Studio or your favorite text-editor.
  3. Search for the word mimeMap, you should find lots of 'em.
  4. Add the following line to the top of the list: .
ndmeiri
  • 4,979
  • 12
  • 37
  • 45
pratik godha
  • 73
  • 1
  • 8