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.
例如,我将使用ImageCompressMiddleware作为PNG文件,而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类型填充或删除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
});
.
.
.
}
转到此链接以获取更多信息: microsoft
Go to this link for more information: microsoft
这篇关于ASP.NET Core如何确定MIME类型并应用不同的中间件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!