Thomas
August
Ryan

Work Posts Résumé

Serve GeoJSON Files in ASP.NET Core 6

By default ASP.NET Core 6 will not expose files with the .geojson file extension in its wwwroot folder. To remedy this issue, we need to add an additional Content Type Provider in our Startup.cs file. Start by constructing a new FileExtensionContentTypeProvider and then adding a mapping for “.geojson” with a value of “application/geo+json” for the content type in your Configure method.

Then add or modify the app.UseStaticFiles(); call to take a new StaticFileOptions with a ContentTypeProvider set to the custom Content Type Provider that you created above. Now you can reference GeoJSON files stored in your project’s wwwroot folder using file paths like “/js/seattle-to-boston.geojson” in your views and razor pages.


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    var provider = new FileExtensionContentTypeProvider();
    provider.Mappings[".geojson"] = "application/geo+json";

    app.UseStaticFiles(new StaticFileOptions
    {
        ContentTypeProvider = provider
    });
}
        

For more info on the why ASP.NET Core does not serve GeoJSON out of the box check out this Github issue and for more on the Static Files Middleware check out the docs.