Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a json file as content to a MVC 4 Web Application

I have a json file that I would like to include as content to my MVC 4 Web Application. I add the file to my Content folder and mark 'Build Action' as 'Content' and 'Copy to Output Directory' as 'Do not copy'. Now if I browse to http://[my site]/content/myjsonfile.json I get an HTTP 404. I mess around with all of the build actions and copy to output selections and still doesn't work. I change the json file extension to txt and then it works. Is this a bug? My site is published using Windows Azure. Thanks for any feedback.

like image 675
user593733 Avatar asked May 21 '13 11:05

user593733


People also ask

How pass data from model to controller in MVC?

In Solution Explorer, right-click the Controllers folder and then click Add, then Controller. In the Add Scaffold dialog box, click MVC 5 Controller with views, using Entity Framework, and then click Add. Select Movie (MvcMovie. Models) for the Model class.

What is @model in Cshtml?

The @model directive allows access to the list of movies that the controller passed to the view by using a Model object that's strongly typed. For example, in the Index.cshtml view, the code loops through the movies with a foreach statement over the strongly typed Model object: CSHTML Copy.

Can we use web forms in MVC?

Luckily, the answer is yes. Combining ASP.NET Webforms and ASP.NET MVC in one application is possible—in fact, it is quite easy. The reason for this is that the ASP.NET MVC framework has been built on top of ASP.NET. There's actually only one crucial difference: ASP.NET lives in System.


1 Answers

The problem is that your webserver doesn't recognize the filetype. Add this line to your web.config:

<system.webServer>
    <staticContent>
      <mimeMap fileExtension=".json" mimeType="application/json; charset=UTF-8" />

    </staticContent>
</system.webServer>
like image 120
Raimond Kuipers Avatar answered Oct 16 '22 23:10

Raimond Kuipers