【问题标题】:Cannot load .geojson file with D3 in ASP.NET Core无法在 ASP.NET Core 中使用 D3 加载 .geojson 文件
【发布时间】:2017-11-12 21:29:04
【问题描述】:

我正在尝试使用 .geojson 文件使用 D3.js 创建世界地图。但是每次页面加载时它都会给我一个404 error http://localhost:xxxx/world.geojson file not found

这是我的 site.js 文件:

$(function() {
   d3.json("./world.geojson", createMap);
   function createMap(countries) {
      var aProjection = d3.geoMercator();
      var geoPath = d3.geoPath().projection(aProjection);
      d3.select("svg").selectAll("path").data(countries.features)
        .enter()
        .append("path")
        .atrt("d", geoPath)
        .attr("class", "countries");
    }
});

这是我的项目结构的样子:structure

任何帮助将不胜感激。谢谢!

【问题讨论】:

    标签: javascript c# asp.net d3.js asp.net-core


    【解决方案1】:

    我可以通过将以下内容添加到我的Startup.cs 来解决我的问题:

    app.UseStaticFiles();
            app.UseStaticFiles(new StaticFileOptions()
            {
                ServeUnknownFileTypes = true,
                FileProvider =
                    new PhysicalFileProvider(
                        Path.Combine(Directory.GetCurrentDirectory(), @"MyStaticFiles")),
                RequestPath = new PathString("/StaticFiles")
            });
    

    我在我的应用程序根目录 (MyStaticFiles) 中创建了一个文件夹,并将我的 .geojson 文件放入其中。然后该文件已正确加载到浏览器中。

    【讨论】:

      【解决方案2】:

      您需要在 Web.config 中为 geojson 和其他“不太常见”的扩展添加 mimeMap,以便告诉 IIS 将它们作为静态资源提供服务:

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

      【讨论】:

        猜你喜欢
        • 2015-02-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-09-08
        • 2017-07-24
        • 1970-01-01
        • 2020-04-27
        • 1970-01-01
        相关资源
        最近更新 更多