ASP.NET Core 如何确定 MIME 类型并应用不同的中间件? [英] How does ASP.NET Core determine MIME types and apply different middleware?
问题描述
我使用 ASP.NET Core MVC 和 .NET Core 2.0.
I use ASP.NET Core MVC and .NET Core 2.0.
我有一些静态文件,它们有不同的文件类型,JPEG、PNG、BMP ...
I have some static files, they have different file types, JPEG, PNG, BMP ...
我想根据不同的文件类型应用不同的中间件.
I would like to apply different middleware according to different file types.
比如PNG文件我会用ImageCompressMiddleware,BMP文件我会用ImageConvertMiddleware.
Such as PNG file I will use ImageCompressMiddleware, BMP file I will use ImageConvertMiddleware.
ASP.NET Core 如何确定 MIME 类型并应用不同的中间件?
How does ASP.NET Core determine MIME types and apply different middleware?
或者根据文件扩展名.
推荐答案
在 configure 部分创建一个 FileExtensionContentTypeProvider 对象,并为每个 MIME Type 填充或移除 Mapping 如下:
Create a FileExtensionContentTypeProvider object in configure section and fill or remove Mapping for each MIME Type as follows:
public void Configure(IApplicationBuilder app)
{
// Set up custom content types -associating file extension to MIME type
var provider = new FileExtensionContentTypeProvider();
// Add new mappings
provider.Mappings[".myapp"] = "application/x-msdownload";
provider.Mappings[".htm3"] = "text/html";
provider.Mappings[".image"] = "image/png";
// Replace an existing mapping
provider.Mappings[".rtf"] = "application/x-msdownload";
// Remove MP4 videos.
provider.Mappings.Remove(".mp4");
app.UseStaticFiles(new StaticFileOptions()
{
FileProvider = new PhysicalFileProvider(
Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot", "images")),
RequestPath = new PathString("/MyImages"),
ContentTypeProvider = provider
});
.
.
.
}
转到此链接了解更多信息:微软
Go to this link for more information: microsoft
这篇关于ASP.NET Core 如何确定 MIME 类型并应用不同的中间件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!