如何在 ASP.NET Core 中添加 Mime 类型 [英] How to add Mime Types in ASP.NET Core

查看:85
本文介绍了如何在 ASP.NET Core 中添加 Mime 类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 .NET Framework 4.6 (MVC4/5) 开发应用程序时,我曾经在 web.config 文件中添加自定义 mime 类型,如下所示(这是我需要在我的应用程序中添加的实际 mime 类型):

When developing an application using .NET Framework 4.6 (MVC4/5), I used to add custom mime types in the web.config file, like this (this is the actual mime types I need to add in my app):

<system.webServer>
  <staticContent>
    <mimeMap fileExtension=".wasm" mimeType="application/wasm"/>
    <mimeMap fileExtension="xap" mimeType="application/x-silverlight-app"/>
    <mimeMap fileExtension="xaml" mimeType="application/xaml+xml"/>
    <mimeMap fileExtension="xbap" mimeType="application/x-ms-xbap"/>
  </staticContent>

如何在 .NET Core 中复制此行为?可以用同样的方法吗?

How can I replicate this behaviour in a .NET Core? Is it possible to do it the same way?

推荐答案

此配置适用于 Web 服务器,不适用于 ASP.NET.web.config 中的 system.webServer 部分处理 IIS 的配置.

This configuration is for the web server, not for ASP.NET. The system.webServer section in the web.config deals with the configuration of IIS.

如果您的 ASP.NET Core 应用程序在 IIS 后面运行并且 IIS 正在处理静态内容,那么您应该继续使用相同的内容.

If your ASP.NET Core application is running behind IIS and IIS is handling the static content, then you should continue to use the same thing.

如果您使用的是 nginx,您可以将 MIME 类型添加到您的配置中或编辑 mime.types 文件.如果您使用不同的网络服务器,请查阅网络服务器的文档.

If you are using nginx, you can add mime types to your configuration or edit the mime.types file. If you are using a different web server, consult the documentation for the web server.

如果 ASP.NET Core 本身正在处理静态内容并在边缘运行,或者如果您需要 ASP.NET Core 了解 mime 类型,则需要配置 ASP.NET Core 的处理程序以了解它.这是解释 在文档中.

If ASP.NET Core is handling the static content itself and is running at the edge, or if you need ASP.NET Core to be aware of mime types, you need to configure ASP.NET Core's handler to be aware of it. This is explained in the documentation.

文档中的示例:

public void Configure(IApplicationBuilder app)
{
    var provider = new FileExtensionContentTypeProvider();
    // Add new mappings
    provider.Mappings[".myapp"] = "application/x-msdownload";

    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "images")),
        RequestPath = "/StaticContentDir",
        ContentTypeProvider = provider
    });

这篇关于如何在 ASP.NET Core 中添加 Mime 类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆