Need To Get Static JSON File with POST verb on IIS 7.5?

Normally, when we stick a JSON file up on an IIS web server, all we have to do to get to is is to set the Mime type.  One easy way to do it is to add to your web.config the following:

 

<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
...

 

This works great as long as the GET verb is used (or just enter the on the url like http://mysite.com/myfile.json).

So, what if you "need” to use the POST keyword.  Say for example, you can not change the JavaScript file to use GET instead of POST to get the file.  I was hoping to find some simple web.config parameter to set but I had no luck.  I even posted to StackOverflow and so far, the answers have been less than helpful.  Who knows, maybe by the time you read this, there will be a better answer then my solution which is to write a simple asp.net handler and register it to type json.

 

So, here is the simpler handler that does the trick:

 

public class JSONHandler : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
string output = System.IO.File.ReadAllText(context.Request.PhysicalPath);
context.Response.Write(output);

}

public bool IsReusable
{
get
{
return false;
}
}
}

 

and the associated web.config entries

 

<httpHandlers>
<add verb="*" path="*.json" validate="false" type="JSONHandler" />
</httpHandlers>
</system.web>

<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<staticContent>
<mimeMap fileExtension=".json" mimeType="application/json" />
</staticContent>
<handlers>
<add verb="*" path="*.json" name="JSONHandler" type="JSONHandler"/>
</handlers>
</system.webServer>

I’m assuming there is a simple way, but for now, this works for me.  Please post a better solution and reference it or just tell me in the comments.

About Peter Kellner

Peter is a software professional specializing in mobile and web technologies. He has also been a Microsoft MVP for the past 7 years. To read more about Peter Kellner and his experience click here. For information about how Peter Kellner might be able to help you with your project click here.

Follow me:


Comments

  1. Ran into this issue. Finally figured out to add a mime type for “.json” with “application/json”, and then a “handler mapping” of type “script map” with “*.json”, “C:\Windows\System32\inetsrv\asp.dll”, and “JSON”. It defaults to all verbs so then POST is handled.

Follow

Get every new post delivered to your Inbox

Join other followers: