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

明信片发生器组装了一个包供网站管理员使用,以便为许多不同的设备提供图标。该页面附带一个名为 site.manifest的文件,该文件通过网页文档 <head>中的以下标签链接到:

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

根据 Mozilla: ”Web 应用程序清单在 JSON 文本文件中提供关于应用程序的信息(如名称、作者、图标和描述)。清单的目的是将网络应用程序安装到设备的主屏幕上,为用户提供更快的访问速度和更丰富的体验。”

不幸的是,如果您正在使用 Microsoft 的 Internet Information Services (IIS) ,那么如果您尝试访问 site.webmanifest文件,将得到一个404.3错误。

确切的错误消息如下: ”由于扩展配置,无法为您请求的页面提供服务。如果页是脚本,则添加处理程序。如果文件应该被下载,添加一个 MIME 映射。”

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

36880 次浏览

By default, IIS does not serve any files that does not have a MIME map associated with it in its (IIS) core settings.

To address this challenge, you will need to map the .webmanifest file extension to its appropriate MIME type.

To accomplish this, open IIS and follow the steps below;

  1. On the left hand side, select either your web site or the entire server in the "Connections" menu. If you select the server, your MIME mapping will apply to every web site on the server. If you select a web site, it will only apply to a single web site.

  2. Next, select "MIME Types" from the IIS menu:

MIME Types Menu Item

  1. Once there, click "add..." from the right hand menu.

  2. In the dialog box that opens specify .webmanifest in the file name extension box application/manifest+json in the MIME type box.

Add MIME Type dialog box

  1. Click "OK".

Congratulations; you've just defined the MIME type for .webmanifest on IIS.

Easier solution is to rename your manifest file to site.webmanifest.json and link as

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

IIS should already have a MIME Type for .json files This is also helpful if deploying to Azure where its not so easy to change the IIS settings.

For Azure I added this as the 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>

For those using ASP.NET Core (I am using 2.1) you can configure the MIME types that can be served in the application Startup.cs file as per the static files docs:

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();
}

Adding to @Ben's answer: if you have a SPA you should put StaticFileOptions code into the UseSpaStaticFiles() call:

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


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

I found that the IIS server had ".json" listed in the Request Filtering feature saying it was not allowed.

enter image description here

Removing that allowed the file to be served.