Deploying a custom static folder under wwwroot

3.5k Views Asked by At

We have a custom folder under wwwroot named Contents with some inner folders shaping a structure for organizing later uploadable files, in the publish mode it is not deployed (On local or server)

Here is part of our code which you may be interested about:

app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(
        Path.Combine(Directory.GetCurrentDirectory(), "Contents")),
    RequestPath = "/Contents"
});

Any suggestions and solutions? Any idea why it is not deployed?

Update:

In .csProj we have this:

  <ItemGroup>
    <Folder Include="Contents\H2X\" />
    <Folder Include="ServerContent\H123\" />
  </ItemGroup>  

We have H2X.publishSettings file, which is generated by the server and contains some of our credentials.

   msdeploySite="213" 
   destinationAppUrl="123" 
   profileName="Default Settings" 
   publishMethod="MSDeploy" userName="123" 
   AllowUntrustedCertificate="True"

Is there anything missed?

I was a bit away from development for a while, now my developer seems stuck with it, wanted to help him.

2

There are 2 best solutions below

1
Sabir Hossain On BEST ANSWER

If the Contents folder inside the wwwroot then add

<Content Include="wwwroot\**\*" CopyToPublishDirectory="PreserveNewest" />

and you no need to configure the Contents folder as static file provider in Startup.cs, above line will include all sub folders and files inside wwwroot.

And if the Contents folder outside of wwwroot then add

<Content Include="Contents\**\*" CopyToPublishDirectory="PreserveNewest" />

and you need to configure the Contents folder as static file provider in Startup.cs

1
SRQ Coder On

Within the ASP.NET Core hosting environment, there are two properties frequently used in building a physical path to a file or directory.

_hostingEnvironment.WebRootPath maps directly to the default wwwroot directory:

_hostingEnvironment.ContentRootPath maps one level higher, usually where the startup.cs file is located.

For file uploads directly into a folder located within the wwwroot folder, use WebRootPath in building the path you need.