16

I want to have a .well-known directory in my root for letsencrypt renewals.

I have added a route to .well-known like so:

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

I added a direcotry inside my wwwroot called .well-known but it never gets copied to the output when I publish.

I tried adding a file to it and also edit csproj:

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

Every time I publish I have to manually create the directory. How do I get it automatically added to the wwwroot?

Guerrilla
  • 13,375
  • 31
  • 109
  • 210
  • Publish does not include empty folders, can you maybe put a dummy.txt file and include it in the project? – Rob Apr 02 '17 at 09:50
  • yeah, I tried that `I tried adding a file to it ` sorry, should have been clearer – Guerrilla Apr 02 '17 at 09:50
  • Did you also include it in the Visual Studio Project and set it's Build Action to `Content`? – Rob Apr 02 '17 at 09:51
  • I just tried this now. When I select "copy always" a warning symbol appears next to it and the project will not build. I think it is because windows won't let you create a folder with a `.` as the first character – Guerrilla Apr 02 '17 at 10:16
  • Where you call app.UseStaticFiles in your question, can anybody confirm that this definitely does NOT interfere with the default behaviour of serving static files from within wwwroot? – Dan Harris Feb 13 '18 at 12:25

4 Answers4

13

I tried adding a file to it and also edit csproj:

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

You can't copy over folders via Content, only files. You have to change it to

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

and like mentioned in the comments, you need to put an empty dummy file inside.

Tseng
  • 61,549
  • 15
  • 193
  • 205
  • It works, I had to setup a dummy file and set the content to "Copy if newer" – Raffaeu Jan 27 '18 at 14:14
  • 1
    @Raffaeu: That's exactly the same as above. When you set the properties in the VS IDE to "Copy if newer" it will create an `PreserveNewest>` entry in your csproj – Tseng Jan 27 '18 at 14:20
  • I've also ran into this issue. Although, the additional configuration in the project file works, I'd argue that this `.well-known` folder shouldn't be excluded, and treated differently. Raised [this issue](https://github.com/dotnet/aspnetcore/issues/42071) on the aspnetcore repo – Ties Jun 07 '22 at 12:42
  • The before mentioned issue is considered for .NET 7, and there is an [outstanding PR](https://github.com/dotnet/sdk/pull/26366) to fix this. For .NET 7, this exclusion is not needed anymore :) – Ties Jun 30 '22 at 12:45
11

Another approach is to make a controller - if you have complex rules - or the file varies by domain (as it does for certain types of verification tokens).

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!");
        }
    }
}

You can then just hit .well-known/apple-developer-merchantid-domain-association and get this controller.

Of course you can load the file from disk or whatever you need to do - or have a passthrough.

Simon_Weaver
  • 140,023
  • 84
  • 646
  • 689
  • After many days dealing with this issue I finally adopted your approach. I was unable to serv the android `assetlinks`, I included it as `embedded resource` and got it via assembly.`GetManifestResourceNames` – Fabrice T Aug 21 '20 at 10:46
5

This worked for me...

In csproj file:

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

In 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
});
unsane
  • 81
  • 1
  • 6
3

you can add the below code to the MyProject.csproj file

  <ItemGroup>
    <Content Include=".well-known\acme-challenge\**">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </Content>
  </ItemGroup>
Alper Ebicoglu
  • 8,884
  • 1
  • 49
  • 55