- Hands-On Full:Stack Web Development with ASP.NET Core
- Tamir Dresher Amir Zuker Shay Friedman
- 290字
- 2021-06-10 19:37:24
Configuring the available MIME types
ASP.Net Core serves static files that are of a known MIME type. A MIME type is an identifier of the type of content that a specific file holds. For example, HTML files have a MIME type of text/html, and JPEG image files have a MIME type of image/jpeg.
When a web server serves a file, it attaches its MIME type to the response as an HTTP header named content-type. The browser then, in turn, uses this MIME type to decide on how to interpret the file.
ASP.NET Core recognizes almost 400 mime types automatically. When it runs into an unrecognized file, it will not serve it to the end user and instead return a 404 — Not Found response. This behavior improves the security of your web application and works in the majority of use cases.
However, sometimes, your application needs to send files to end users that are not recognized by default by ASP.NET Core; for example, .exe files, which are Windows' executable file format. The following code instructs ASP.NET Core to use the application/vnd.microsoft.portable-executable MIME type for these types of files:
FileExtensionContentTypeProvider provider = new FileExtensionContentTypeProvider();
provider.Mappings[".exe"] = "application/vnd.microsoft.portable-executable";
StaticFileOptions staticFileOptions = new StaticFileOptions()
{
ContentTypeProvider = provider
};
app.UseStaticFiles(staticFileOptions);
Using this technique, you can also change existing MIME types to fit your needs, or remove MIME types that you do not want to be served. For example, the following code changes .ts files to be recognized as TypeScript files, instead of the video file MIME type defined by ASP.NET Core. This will be needed later in this book when we start using TypeScript:
FileExtensionContentTypeProvider provider = new FileExtensionContentTypeProvider();
provider.Mappings[".ts"] = "application/x-typescript";
StaticFileOptions staticFileOptions = new StaticFileOptions()
{
ContentTypeProvider = provider
};
app.UseStaticFiles(staticFileOptions);
- 計算機網絡與通信(第2版)
- Building E-commerce Sites with VirtueMart Cookbook
- 物聯網安全:理論、實踐與創新
- 物聯網之魂:物聯網協議與物聯網操作系統
- 智慧城市中的移動互聯網技術
- 區塊鏈輕松上手:原理、源碼、搭建與應用
- 電力物聯網工程技術原理與應用
- Metasploit Penetration Testing Cookbook
- 網絡環境中基于用戶視角的信息質量評價研究
- 一本書讀懂物聯網
- Microsoft Power Platform Enterprise Architecture
- 基于IPv6的家居物聯網開發與應用技術
- Laravel Application Development Cookbook
- 工業以太網技術:AFDX/TTE網絡原理、接口、互連與安全
- 趣話通信:6G的前世、今生和未來