在 Blazor 服务器项目上提供没有文件扩展名的静态内容

Serving static content without a file extension on a Blazor server project

根据 Apple 对 Universal Links 的要求,我有一个名为 "apple-app-site-association" 的文件,它位于 azure 网站的根文件夹中。访问 mysite.com/apple-app-site-association 应该 return 浏览器中的 JSON 文本。我在 Azure 上托管网站,并且是 运行 一个 Blazor 服务器项目。我的项目没有 web.config 文件。

要清楚,文件 "apple-app-site-association" 不应具有“.json”

的扩展名

我看过 and

我还尝试修改 Startup.cs 中的 Configure() 方法以提供静态文件

app.UseStaticFiles(new StaticFileOptions
{
    ServeUnknownFileTypes = true,
    DefaultContentType = "application/json"
});

虽然上面的代码确实正确地服务于 mysite.com/apple-app-site-association,但它有一个不需要的副作用,即 404'ing _framework/blazor.server.js.

如何修改 apple-app-site-association 的 MIME 类型,以便我的 Blazor 服务器项目在访问 mysite 时提供文件。com/apple-app-site-association?

或者,使用上面的UseStaticFiles()方法,如何解决加载_framework/blazor.server.js时的404错误?

在_Host.cshtml

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <base href="~/" />
    <link rel="stylesheet" href="css/bootstrap/bootstrap.min.css" />
    <link href="css/site.css" rel="stylesheet" />
    <link rel="stylesheet" href="_content/Radzen.Blazor/css/default.css" />
</head>
<body>
    ...some stuff...

    <script src="_framework/blazor.server.js"></script>
</body>
</html>

尽管您使用的是 Blazor,但它本质上仍然是一个 ASP.NET 核心应用程序,问题实际上是关于 ASP.NET 核心、路由以及静态文件的处理方式。

中所示,通过控制器执行此操作可能最简单,而不是试图强制路由器处理没有扩展名的 URL。我还在一个项目中为 robots.txt 完成了此操作,以控制针对不同品牌显示的内容。

我试过这个:

    public class StaticContentController : Controller
    {
        [HttpGet]
        [Route("apple-app-site-association")]
        public ContentResult AppleAppSiteAssociation()
        {
            // source in root of wwwroot folder
            const string source = @"apple-app-site-association.json";
            string json = System.IO.File.ReadAllText(source);
            return Content(json, "application/json", Encoding.UTF8);
        }
    }

源文件(带有 .json 扩展名)在带有 "Copy if newer" 属性 集的项目中,因此它存在于 /bin 文件夹中。

运行: