How to Fix IIS 404 Error When Serving JSON Files: MIME Type Configuration Guide


2 views

Many developers encounter this puzzling scenario: Your IIS server happily serves .js files, but throws 404 errors when accessing identical files with .json extensions. This isn't a file permission or path issue - it's typically a MIME type configuration problem in IIS.

IIS maintains a list of allowed file extensions and their corresponding MIME types. By default, some versions of IIS don't include .json in this list. When IIS encounters an unrecognized extension, it returns 404 instead of serving the file.

Try this quick test in PowerShell to confirm:

# Check if .json extension is registered
Get-WebHandler -RequestPath "*.json" -PSPath "IIS:\Sites\Default Web Site"

If this returns no results, IIS isn't configured to handle JSON files.

The most straightforward fix is to add the JSON MIME type:

# PowerShell command to add MIME type
Add-WebConfigurationProperty -pspath 'MACHINE/WEBROOT/APPHOST' -filter 'system.webServer/staticContent' -name '.' -value @{fileExtension='.json';mimeType='application/json'}

For application-specific configuration, add this to your web.config:

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

For dynamic JSON generation, you might need a handler mapping:

<configuration>
  <system.webServer>
    <handlers>
      <add name="JSONHandler" path="*.json" verb="GET" 
           type="System.Web.Handlers.TransferRequestHandler" 
           resourceType="Unspecified" />
    </handlers>
  </system.webServer>
</configuration>

After making changes:

  1. Run iisreset in command prompt
  2. Clear browser cache
  3. Test with a simple JSON file:
    {
      "status": "OK",
      "message": "JSON should now load properly"
    }
    
  • Check for URL rewrite rules that might interfere
  • Verify Request Filtering isn't blocking .json extensions
  • For IIS Express, modify applicationhost.config instead
  • Consider security implications when exposing JSON files

When IIS returns 404 errors for valid JSON files while serving other extensions (like .js) normally, we're typically dealing with one of these core issues:

// Test case that fails
http://example.com/data/config.json → 404
http://example.com/data/config.js → 200 (same file renamed)

MIME Type Verification: IIS won't serve files with unregistered MIME types. Check your server's configuration:

// PowerShell check for JSON MIME type
Get-WebConfigurationProperty -Filter //staticContent/mimeMap -Name * | 
Where-Object { $_.fileExtension -eq '.json' } | 
Select-Object fileExtension, mimeType

If the above returns nothing, add the JSON MIME type through IIS Manager or directly in web.config:

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

In some IIS configurations, handler mappings might intercept JSON requests. Verify with:

appcmd list config /section:handlers | findstr ".json"

For API scenarios, you might need to explicitly allow static JSON files:

<handlers>
  <add name="JSONHandler" path="*.json" verb="GET" 
       type="System.Web.StaticFileHandler" />
</handlers>

IIS might block JSON files if they're listed in hidden segments or denied extensions:

<security>
  <requestFiltering>
    <fileExtensions allowUnlisted="true">
      <add fileExtension=".json" allowed="true" />
    </fileExtensions>
  </requestFiltering>
</security>

If using compression, ensure JSON is in the dynamic types list:

<httpCompression>
  <dynamicTypes>
    <add mimeType="application/json" enabled="true" />
  </dynamicTypes>
</httpCompression>