将 .well-known 添加到 asp.net core

17

我希望在我的根目录下有一个 .well-known 目录以进行 Let's Encrypt 更新。

我已经添加了一个路由到 .well-known,如下所示:

 app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @".well-known")),
            RequestPath = new PathString("/.well-known"),
            ServeUnknownFileTypes = true // serve extensionless file
        });

我在我的wwwroot中添加了一个名为.well-known的目录,但是当我发布时它从未被复制到输出中。

我尝试向其中添加文件,并编辑csproj:

  <ItemGroup>
    <Content Include="wwwroot\.well-known\" />
  </ItemGroup>

每次我发布内容时,都需要手动创建目录。如何实现自动将其添加到wwwroot中?


发布不包括空文件夹,你可以放一个dummy.txt文件并将其包含在项目中吗? - Rob
是的,我尝试过了。我试图添加一个文件到其中。抱歉,我应该说得更清楚些。 - Guerrilla
你是否也将它包含在Visual Studio项目中,并将其构建操作设置为“内容”? - Rob
我刚刚尝试了一下。当我选择“始终复制”时,旁边会出现一个警告符号,并且项目将无法构建。我认为这是因为Windows不允许您创建以作为第一个字符的文件夹。 - Guerrilla
在你的问题中调用app.UseStaticFiles时,有人可以确认这绝对不会干扰从wwwroot内提供静态文件的默认行为吗? - Dan Harris
4个回答

13
我试着将一个文件添加进去,并编辑了csproj文件:

我试着将一个文件添加进去,并编辑了csproj文件:

<ItemGroup>
  <Content Include="wwwroot\.well-known\" />
</ItemGroup>

您无法通过“内容”复制文件夹,只能复制文件。您需要将其更改为

<ItemGroup>
  <Content Include="wwwroot\**">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </Content>
<ItemGroup>

如评论中所提到的,您需要放置一个空的虚拟文件。


它能正常工作,我必须设置一个虚拟文件并将内容设置为“如果较新则复制”。 - Raffaeu
1
@Raffaeu:这与上面完全相同。当您在VS IDE中将属性设置为“仅在更新时复制”时,它将在您的csproj中创建一个<Content Include="<filename or folder>"><CopyToOutputDirectory>PreserveNewest></CopyToOutputDirectory>条目。 - Tseng
我也遇到了这个问题。虽然在项目文件中进行额外配置可以解决,但我认为 .well-known 文件夹不应该被排除并且需要特殊处理。已在 aspnetcore 存储库上提出了此问题 - Ties
上述问题将在.NET 7中得到考虑,同时也有一个未解决的PR来修复这个问题。在.NET 7中,这个排除将不再需要 :) - Ties

11
另一种方法是创建一个控制器,如果您有复杂的规则,或者文件因域而异(对于某些类型的验证令牌),则适用。
public class WellKnownFileController : Controller
{
    public WellKnownFileController()
    {

    }

    [Route(".well-known/apple-developer-merchantid-domain-association")]
    public ContentResult AppleMerchantIDDomainAssociation()
    {
        switch (Request.Host.Host)
        {
            case "www2.example.com":
                return new ContentResult
                {
                    Content = @"7B227073323935343637",
                    ContentType = "text/text"
                };

            default:
                throw new Exception("Unregistered domain!");
        }
    }
}

你只需要访问.well-known/apple-developer-merchantid-domain-association就能获取该控制器。

当然,你也可以从磁盘加载文件或进行其他必要操作,或者使用透传方式。


经过多天的处理,我终于采用了您的方法。我无法提供Android assetlinks,但我将其作为“嵌入式资源”包含,并通过程序集的GetManifestResourceNames获取它。 - Fabrice T

5

这对我起作用了...

在 csproj 文件中:

<ItemGroup>
    <Content Include="wwwroot\.well-known\**">
      <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

在 Startup.cs 文件中。
app.UseStaticFiles(); // <-- don't forget this
app.UseStaticFiles(new StaticFileOptions
{
     FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"wwwroot/.well-known")),
     RequestPath = new PathString("/.well-known"),
     ServeUnknownFileTypes = true
});

3

您可以将以下代码添加到MyProject.csproj文件中:

  <ItemGroup>
    <Content Include=".well-known\acme-challenge\**">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>

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