如何让 IIS 在我的网站上正确提供 .webmanifest 文件?

How Can I have IIS properly serve .webmanifest files on my web site?

Favicon Generator 组装了一个供网站管理员使用的软件包,以便为许多不同的设备提供可用的图标。该页面附带一个名为 site.manifest 的文件,该文件通过网页文档 <head>:

中的以下标记链接到
<link rel="manifest" href="site.webmanifest">

根据Mozilla"The web app manifest provides information about an application (such as name, author, icon, and description) in a JSON text file. The purpose of the manifest is to install web applications to the homescreen of a device, providing users with quicker access and a richer experience."

不幸的是,如果您使用的是 Microsoft 的 Internet 信息服务 (IIS),则在尝试访问 site.webmanifest 文件时会收到 404.3 错误。

具体报错信息如下:"The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map."

如何在 IIS 中正确提供 site.webmanifest 文件?

默认情况下,IIS 不提供任何在其 (IIS) 核心设置中没有与之关联的 MIME 映射的文件。

要应对这一挑战,您需要将 .webmanifest 文件扩展名映射到其适当的 MIME 类型。

为此,请打开 IIS 并执行以下步骤;

  1. 在左侧的 "Connections" 菜单中,select 您的网站或整个服务器。 如果您 select 服务器,您的 MIME 映射将应用于服务器上的每个网站。 如果您 select 一个网站,它将只适用于一个网站。

  2. 下一步,select "MIME Types" 来自 IIS 菜单:

  1. 在那里,单击右侧菜单中的 "add..."。

  2. 在打开的对话框中,在文件扩展名框中指定.webmanifest,在MIME类型框中指定application/manifest+json

  1. 点击"OK"。

恭喜;您刚刚在 IIS 上为 .webmanifest 定义了 MIME 类型。

更简单的解决方案是将清单文件重命名为 site.webmanifest.json 并将 link 重命名为

 <link rel="manifest" href="site.webmanifest.json">

IIS 应该已经有 .json 文件的 MIME 类型 如果部署到更改 IIS 设置不太容易的 Azure,这也很有用。

对于 Azure,我将其添加为 web.config

<?xml version="1.0"?>

<configuration>
    <system.webServer>
        <staticContent>
            <mimeMap fileExtension=".json" mimeType="application/json" />
            <mimeMap fileExtension=".webmanifest" mimeType="application/manifest+json" />
        </staticContent>
    </system.webServer>
</configuration> 

对于那些使用 ASP.NET Core(我使用的是 2.1)的用户,您可以根据 static files 文档配置可以在应用程序 Startup.cs 文件中提供的 MIME 类型:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    FileExtensionContentTypeProvider provider = new FileExtensionContentTypeProvider();
    provider.Mappings[".webmanifest"] = "application/manifest+json";

    app.UseStaticFiles(new StaticFileOptions()
    {
        ContentTypeProvider = provider
    });

    app.UseMvc();
}

添加到@Ben 的:如果您有 SPA,则应将 StaticFileOptions 代码放入 UseSpaStaticFiles() 调用中:

FileExtensionContentTypeProvider provider = new FileExtensionContentTypeProvider();
provider.Mappings[".webmanifest"] = "application/manifest+json";

app.UseSpaStaticFiles(new StaticFileOptions()
{
    ContentTypeProvider = provider
});

我发现 IIS 服务器在请求过滤功能中列出了“.json”,表示不允许。

删除允许提供文件。