将 .well-known 添加到 asp.net 核心
Add .well-known to asp.net core
我想在我的根目录中有一个 .well-known
目录用于 letsencrypt 续订。
我添加了一条通往 .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?
I tried adding a file to it and also edit csproj:
<ItemGroup>
<Content Include="wwwroot\.well-known\" />
</ItemGroup>
您不能通过内容复制文件夹,只能复制文件。您必须将其更改为
<ItemGroup>
<Content Include="wwwroot\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<ItemGroup>
就像评论中提到的,你需要在里面放一个空的虚拟文件。
您可以将以下代码添加到 MyProject.csproj
文件
<ItemGroup>
<Content Include=".well-known\acme-challenge\**">
<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
即可获得此控制器。
当然你可以从磁盘加载文件或任何你需要做的事情——或者有一个直通。
这对我有用...
在 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
});
我想在我的根目录中有一个 .well-known
目录用于 letsencrypt 续订。
我添加了一条通往 .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?
I tried adding a file to it and also edit csproj:
<ItemGroup> <Content Include="wwwroot\.well-known\" /> </ItemGroup>
您不能通过内容复制文件夹,只能复制文件。您必须将其更改为
<ItemGroup>
<Content Include="wwwroot\**">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<ItemGroup>
就像评论中提到的,你需要在里面放一个空的虚拟文件。
您可以将以下代码添加到 MyProject.csproj
文件
<ItemGroup>
<Content Include=".well-known\acme-challenge\**">
<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
即可获得此控制器。
当然你可以从磁盘加载文件或任何你需要做的事情——或者有一个直通。
这对我有用...
在 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
});