簡體   English   中英

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

[英]Add .well-known to asp.net core

我想在我的根目錄中有一個.well-known目錄用於 letencrypt 續訂。

我添加了一條到.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?

我嘗試向其中添加一個文件並編輯 csproj:

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

您不能通過內容復制文件夾,只能復制文件。 你必須把它改成

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

就像評論中提到的那樣,你需要在里面放一個空的虛擬文件。

另一種方法是創建一個控制器——如果你有復雜的規則——或者文件因域而異(就像某些類型的驗證令牌一樣)。

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並獲得這個控制器。

當然,您可以從磁盤或任何您需要做的事情加載文件 - 或者進行直通。

您可以將以下代碼添加到MyProject.csproj文件中

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

這對我有用...

在 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
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM