从 root 以外的地方服务 favicon.ico

Serve favicon.ico from somewhere other than root

ASP.NET 核心 2.1.

我的图标位于 /images/favicon.ico,无法更改。所以页面包括:

<link href="/images/favicon.ico" rel="shortcut icon" type="image/x-icon" />

那行得通。

但是直接发送到 example.com/favicon.ico 的请求将失败并显示 404。

如何将 example.com/favicon.ico 重定向到 example.com/images/favicon.ico

在你的Startupclass的Configure方法中,添加如下代码

RewriteOptions rewriteOptions = new RewriteOptions().AddRedirect("favicon.ico", "images/favicon.ico");

app.UseRewriter(rewriteOptions);

有关详细信息,请参阅 this page