AspNetKatana icon indicating copy to clipboard operation
AspNetKatana copied to clipboard

FileExtensionContentTypeProvider does not include JSON files - reasoning?

Open AlexUrbanAc opened this issue 2 years ago • 2 comments

Hello,

while using the StaticFilesMiddleware I noticed that the ".json" extension is not known in the FileExtensionContentTypeProvider, thus JSON files are not delivered if "ServeUnknownFileTypes" is set to false.

Is this a similar oversight like how old versions of IIS (IIRC Windows Server 2008 R2) did not serve .json files by default, while newer versions (IIRC Windows Server 2013) do so, or did you leave that file type out for a good reason?

Kind regards,

Alexander

AlexUrbanAc avatar Mar 23 '22 16:03 AlexUrbanAc

The list was copied from IIS a long time ago and never updated. We try to avoid adding everything, but json is a reasonable one.

This was added to ASP.NET Core: https://github.com/dotnet/aspnetcore/blob/4686703ff8ef199580300e8056142abe44311a1b/src/Middleware/StaticFiles/src/FileExtensionContentTypeProvider.cs#L150

Would you like to send a PR?

Tratcher avatar Mar 23 '22 17:03 Tratcher

You could (temporarily at least) either add a passthrough provider including JSON or proxy the ASP.NET Core 2.2 version. I did a very rough proof of concept for that here.

<:thought_balloon:> It is a pity, in some respects, that the separate nuget packages targeting netstandard were dropped, or that some of these kinds of facilities weren't kept in a separate package altogether. I could definitely see scenarios where inferring the content type from a file extension would be useful - a command line or even MAUI (batch) upload to azure blob storage comes immediately to mind. You can see this kind of issue at https://github.com/dotnet/aspnetcore/blob/main/src/Components/WebView/WebView/src/FileExtensionContentTypeProvider.cs where it even comments of this problem in the source of the class

// NOTE: This file is copied from src/Middleware/StaticFiles/src/IContentTypeProvider.cs
// and made internal with a namespace change.
// It can't be referenced directly from the StaticFiles package because that would cause this package to require
// Microsoft.AspNetCore.App, thus preventing it from being used anywhere ASP.NET Core isn't supported (such as
// various platforms that .NET MAUI runs on, such as Android and iOS).

There is also the duplication of the mediatype/content type strings in the MediaTypeNames class in the System.Net.Mime Namespace. I have often thought it strange that there isn't a good/comprehensive single 'source of truth' for these constants (preferably in an updatable package) as part of the primary .net ecosystem. By primary I mean directly supported/published by Microsoft (or I guess DNF). </:thought_balloon:>

CZEMacLeod avatar Mar 29 '22 16:03 CZEMacLeod