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

查看:689
本文介绍了如何在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核心应用程序在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 文件。如果您使用其他Web服务器,请查阅该Web服务器的文档。

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天全站免登陆