ASP.NET Core如何确定MIME类型并应用不同的中间件?

编程入门 行业动态 更新时间:2024-10-24 20:12:42
本文介绍了ASP.NET Core如何确定MIME类型并应用不同的中间件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用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类型并应用不同的中间件?

本文发布于:2023-11-05 09:50:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560518.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中间件   类型   NET   ASP   MIME

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!