AspNetCore does not serve unknown file types for security reasons, but it should generally be safe to do for files in the .well-known folder.
The problem is that Apple requires the correct MIME type for files without extensions, so we must explicitly configure those.
Solution
- Allow serving unknown file types (no extension) from
.well-known folder.
- Add files to
wwwroot/.well-known folder.
- Add a custom
IContentTypeProvider to map filenames to MIME type, such as apple-app-site-association file name to application/json MIME type.
Startup.cs
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(env.ContentRootPath, "wwwroot", ".well-known")),
RequestPath = "/.well-known",
ServeUnknownFileTypes = true,
ContentTypeProvider = new CustomWellKnownFileContentTypeProvider(),
});
CustomWellKnownFileContentTypeProvider.cs
/// <summary>
/// Custom file extension content type provider for serving certain extension-less files in .well-known folder.
/// <list type="bullet">
/// <item>apple-app-site-association - application/json - Apple Universal Link association</item>
/// </list>
/// </summary>
public class CustomWellKnownFileContentTypeProvider : IContentTypeProvider
{
private readonly FileExtensionContentTypeProvider _defaultProvider = new();
public bool TryGetContentType(string subpath, out string contentType)
{
// Custom handling of files without file extensions.
// Apple Universal Link association, requires the correct MIME type set.
if (subpath.EndsWith("apple-app-site-association", StringComparison.OrdinalIgnoreCase))
{
contentType = "application/json";
return true;
}
// Fallback to default provider, based on file extension.
if (_defaultProvider.TryGetContentType(subpath, out string? extContentType))
{
contentType = extContentType;
return true;
}
contentType = string.Empty;
return false;
}
}