如何在Asp .Net Core中为带有句点的目录名提供静态文件服务

3

我目前正在尝试将Apple Pay与网站相关联。为了验证域,Apple Pay要求网站能够在https://websiteurl/.well-known/apple-developer-merchantid-domain-association提供特定的静态文件。

我目前无法让我的网站提供这个静态文件。我可以提供其他静态文件,但就是不能提供这个特定的文件。我想问题可能和目录名称中的句号有关。我已经尝试了这个问题中的建议,但没有成功。

我的wwwroot目录结构如下:

wwwroot
    .well-known
        apple-developer-merchantid-domain-association
        web.config
    App_Data
    Content
    css
    js
        app.js
    lib

.well-known文件夹的内容->web.config是:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <staticContent>
      <mimeMap fileExtension="." mimeType="application/octet-stream" />
    </staticContent>
  </system.webServer>
</configuration>

我认为这就是此问题中被接受答案所描述的情况。

然而,当我尝试访问https://websiteurl/.well-known/apple-developer-merchantid-domain-association时,我得到了一个HTTP 404错误。

我已经验证成功地访问静态文件,因为https://websiteurl/js/app.js可以正常工作。

我还尝试将web.config直接放在wwwroot下面,但不确定我做错了什么。有什么建议吗?


如果您为文件添加扩展名,它是否可以带有该扩展名进行下载? - Martin Staufcik
1
不,苹果要求目录和文件名必须与我在问题中发布的完全一致。 - Mike Moore
1
@Mike;如果回答有帮助,请标记为已接受的答案(即使是你自己的)。 - XAMT
2个回答

7
原来问题不是目录名中的句号,而是文件没有扩展名。在Startup.cs中添加以下代码解决了问题:
    app.UseStaticFiles();
    app.UseStaticFiles(new StaticFileOptions    
    {
         FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/.well-known")),
         RequestPath = "/.well-known",
         ServeUnknownFileTypes = true,
         DefaultContentType = "text/plain"
     });

我认为应该是PhysicalFileProvider而不是physicalFileProvider。 - Dave Barnett

0

尝试使用FileServer:

public void Configure(IApplicationBuilder app)
{
    app.UseStaticFiles();

    app.UseFileServer(new FileServerOptions
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(Directory.GetCurrentDirectory(), "MyStaticFiles")),
        RequestPath = "/wwwroot",
        EnableDirectoryBrowsing = true
    });
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接