【问题标题】:How to serve static file from Asp .Net Core with period in directory name如何使用目录名称中的句点从 Asp .Net Core 提供静态文件
【发布时间】:2020-04-09 18:28:01
【问题描述】:

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

我目前无法让我的网站提供此静态文件。我能够提供其他静态文件,但不是这个特定的。我想这个问题与目录名称中的句点有关。我尝试遵循this question 的建议,但没有成功。

我的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>

我相信这就是this question 中接受的答案中描述的内容。

但是,当我尝试访问 https://websiteurl/.well-known/apple-developer-merchantid-domain-association 时,我收到了 HTTP 404 错误。

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

我也试过把web.config直接放在wwwroot下面

不知道我做错了什么。有什么建议吗?

【问题讨论】:

  • 如果给文件一个扩展名,可以下载扩展名吗?
  • 不,Apple 要求目录和文件名与我在问题中发布的完全相同。
  • @Mike;如果有帮助,请将答案标记为已接受(甚至是您的)。

标签: asp.net-core applepay


【解决方案1】:

原来问题不是目录名称中的句点,而是文件缺少扩展名。 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。
【解决方案2】:

试试文件服务器:

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

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

【讨论】:

    猜你喜欢
    • 2020-02-24
    • 2018-02-05
    • 1970-01-01
    • 2020-01-31
    • 2020-04-18
    • 2021-09-29
    • 2012-12-09
    • 2012-09-30
    • 1970-01-01
    相关资源
    最近更新 更多