Working with a static .json file located in App_Data?

In my ASP.NET MVC 4 project, I have a .json file in my App_Data folder containing the geographic data that I want to upload to D3.js.

So far, my working approach has been for jQuery to make an AJAX call to some controller that returns JsonResult - and if successful, storing JSON in some Javascript variable that loads in D3. This time I would like to skip the controller and request a static .json file directly from the App_Data folder.

I tried to capture the relative .json path using var url = "@Url.Content("~/App_Data/example.json")"; but the Javascript debugger overtook me with a lot of weird regex errors.

I also tried dropping the file into the Content folder to find out if the directory name matters.

  • var path = "@Url.Content("~/Content/example.json")"; Led to

    NetworkError: 404 Not Found - localhost: xxxxx / Content / u.json

  • var path = @Url.Content("~/Content/example.json"); Led to

    SyntaxError: invalid regular expression flag u: var path = / Content / example.json;

  • var json = $.getJSON("../Content/example.json") appears to send the request to the correct directory, but returns a 404 error. In addition, using Razor syntax to specify a relative URL works, but still 404s.
  • Adding mimeMap information to web.config also did not help.

My question is: is it possible to work with a JSON file stored in App_Data (or in the Content directory) using only Javascript / jQuery? In ASP.NET, is there only one way to do this? Is there a better approach to take at all?

+6
source share
4 answers

The problem was caused by the problem with IIS Express , and this post helped me solve the problem . I switched to

 C:\Users\<username>\Documents\IISExpress\config\applicationhost.config 

and added

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

to the <staticContent> section. It is not enough to add this line to web.config. Thus, you can work with a static .json file stored in the ../ Content folder.

+2
source

To read json files in visual studio first, you need to use the following in your web.config

you can use it anywhere

 <configuration> <system.webServer> <staticContent> <mimeMap fileExtension="json" mimeType="application/json" /> </staticContent> </system.webServer> -------All other Settings--- ----Your all other setting------ </configuration> 

It is not possible to access App_Data due to security restrictions, but you can put your file elsewhere in application.try using this with the jquery getJSON () function below.

  $("document").ready(function() { var jqxhr = $.getJSON("/Content/usa.json", function () { console.log("success"); }) .done(function () { console.log("second success"); }) .fail(function () { console.log("error"); }) .always(function () { console.log("complete"); }); }); 

Happy coding enjoy

+9
source

"outlined me with a lot of strange regular expression errors and other things" - more detailed information may be needed here.

Have you tried using Razor syntax in a simple javascript file? Razor syntax only works in cshtml files.

The second assumption is that App_Data not served by the web server. This will be a huge security gap - any user can simply download database files from this folder. If you want to make it available statically, put it in Scripts/ or Content/ .

+1
source

For static data, just assign the data to a variable in the js file and place it on the page as a <script> . Pass this variable to your initialization method

0
source

Source: https://habr.com/ru/post/973423/


All Articles