简体   繁体   English

对于具有相同扩展名(在同一文件夹中)的不同文件,我可以有不同的 MIME 类型映射吗?

[英]Can I have different MIME type mappings for different files with the same extension (in the same folder)?

Introduction介绍

I'm configuring MIMEs for static files as usual, something like this (and that works fine, keep reading so you get to the actual question):我像往常一样为静态文件配置 MIME,如下所示(效果很好,请继续阅读,以便您了解实际问题):

var defaultStaticFileProvider = new PhysicalFileProvider(Path.Combine(webHostEnvironment.ContentRootPath, "content"));
var contentTypeProvider = new FileExtensionContentTypeProvider();            
var defaultStaticFilesRequestPath = "/content";
// This serves static files from the 'content' directory.
app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = defaultStaticFileProvider,
    ServeUnknownFileTypes = false,
    RequestPath = defaultStaticFilesRequestPath,
    ContentTypeProvider = contentTypeProvider
});

The previous code is mapping the .json extension to application/json by default.前面的代码默认将.json扩展名映射到application/json Works fine.工作正常。

Question问题

What I want is to change that mapping to application/manifest+json but only for one file: manifest.json我想要的是将该映射更改为application/manifest+json但仅限于一个文件:manifest.json

So, I tried to add another configuration like this (not working):所以,我尝试添加另一个这样的配置(不工作):

// Add custom options for manifest.json only.
var manifestContentTypeProvider = new FileExtensionContentTypeProvider();
manifestContentTypeProvider.Mappings.Clear();
manifestContentTypeProvider.Mappings.Add(".json", "application/manifest+json");
var manifestStaticFileProvider = new PhysicalFileProvider(Path.Combine(webHostEnvironment.ContentRootPath, "content/en/app"));
var manifestStaticFileRequestPath = "/content/en/app/manifest.json";
app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = manifestStaticFileProvider,
    ServeUnknownFileTypes = false,
    RequestPath = manifestStaticFileRequestPath,
    ContentTypeProvider = manifestContentTypeProvider
});

Just to clarify, I have added the above code right after the previous one.为了澄清起见,我在前一个代码之后添加了上面的代码。

Hope the question is clear enough, I'll be checking for comments proposing editing tips to make it better anyways.希望问题足够清楚,我将检查提出编辑技巧的评论,以使其更好。

The StaticFileOptions class has an OnPrepareResponse property to which you can assign an Action in order to change the HTTP response headers. StaticFileOptions类有一个OnPrepareResponse属性,您可以为其分配一个Action以更改 HTTP 响应标头。

From the documentation文档

Called after the status code and headers have been set, but before the body has been written.在设置状态码和标头之后,但在写入正文之前调用。 This can be used to add or change the response headers.这可用于添加或更改响应标头。

In that Action you check for the manifest.json file and set/change the content-type header accordingly.在该Action中,您检查manifest.json文件并相应地设置/更改content-type标头。 That action has an StaticFileResponseContext input argument with access to the HttpContext and File .该操作有一个StaticFileResponseContext输入参数,可以访问HttpContextFile

var options = new StaticFileOptions
{
    OnPrepareResponse = staticFileResponseContext =>
    {
        var httpContext = staticFileResponseContext.Context;

        // Request path check:
        if (httpContext.Request.Path.Equals("/content/en/app/manifest.json", StringComparison.OrdinalIgnoreCase))
        // or file name only check via: 
        // if (staticFileResponseContext.File.Name.Equals("manifest.json", StringComparison.OrdinalIgnoreCase))
        {
            httpContext.Response.ContentType = "application/manifest+json"
        }
    },
    // Your other custom configuration
    FileProvider = defaultStaticFileProvider,
    ServeUnknownFileTypes = false,
    RequestPath = defaultStaticFilesRequestPath
};

app.UseStaticFiles(options);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 同一类的不同EntityTypeConfiguration映射 - Different EntityTypeConfiguration Mappings for Same Class 我们不能为不同的类提供相同名称的扩展方法吗? - Can't we have extension methods with same name for different classes? 如何对具有相同成员的不同类型参数使用相同的函数? - How can I use the same function for different type parameters which have the same member? 我可以在相同名称的不同区域中拥有相同的Blob存储吗? - Can I have same blob storage in different regions with same name? 如何对具有相同类型和名称的变量的不同结构使用通用类型参数? - How can I use a generic type parameter for different structs that have a variable with the same type and name? 查找文件夹中具有相同名称但扩展名不同的文件 - find files with same names but different extensions in a folder .NET Core Automapper 相同类型的不同映射 - .NET Core Automapper different mappings for same types 如何引用具有相同名称的2个不同的DLL? - How can I reference 2 different DLLs that have the same name? 对于2个不同的文件,computehash相同 - computehash same for 2 different files 在目录中搜索一对文件(名称相同,扩展名不同) - Search directory for a pair of files (same name, different extension)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM