How to exclude files from being published in ASP.NET Core?
Asked Answered
S

3

10

I have noticed that when I publish the new ASP.NET project, then it puts all the non-code files in the root folder in packages. E.g. these files end up there:

  • Publish profiles
  • gulpfile.js

There is no real need to include those in the published folder. In older solutions it was just as easy as modifying properties of the files to exclude them. Now properties do something completely different and open a quite useless dialog where you can only see the path of the file. Perhaps it's possible to do some other way? Ideally the IDE should be smart enough not to publish these common files, but for some custom it should be a way to exclude them.

It's of course not so big of a problem that some additional files get published, but it makes sense to exclude them as well.

P.S. The possible proposed duplicate is not the same, since it deals only with the old project/solution structure, while ASP.NET Core introduced a new one where the other solution is not applicable.

Simulated answered 18/12, 2015 at 9:38 Comment(3)
Possible duplicate of Exclude files from web site publish in Visual StudioMetchnikoff
That's not quite a duplicate, the structure of ASP.NET 5 files is different and it might be other proper ways of doing it, especially if you take the build into account (it does that publishing when packaging to nuget as far as I understand). The suggested duplicate is for other types of projects/solutions.Simulated
Indeed it isn't. MVC5 and ASP.NET5 and very different: really unhelpful versioning from MS.Metchnikoff
F
10

By default all code files in a directory containing a project.json are included in the project. You can control this with the include/exclude sections of the project.json.

The most common sections that you will see for including and excluding files are:

{
  "compile": "*.cs",
  "exclude": [
    "node_modules",
    "bower_components"
  ],
  "publishExclude": [
    "**.xproj",
    "**.user",
    "**.vspscc"
  ]
}
  • The compile section specifies that only .cs files will be compiled.
  • The exclude section excludes any files in the node_modules and bower_components directories. Even if sections have .cs extensions.
  • The publishExclude section allows you to exclude files from the publish output of your project. In this example, all .xproj, .user, and .vspscc files from the output of the publish command.

From here

Flunkey answered 18/12, 2015 at 10:37 Comment(3)
How do you exclude a folder? just writing the folder name in the publishExclude seems not working.Sewel
It looks like this has changed a little bit in the latest RC2 bits. publishExclude no longer being an option github.com/aspnet/Announcements/issues/175#include-structureDyal
in the RTM version you can use publishOptions > excludeFilesHandset
M
0

Assuming you are using VS publish profiles:

You can edit the .pubxml file directly (it is just XML) to add elements:

<ExcludeFoldersFromDeployment>
  images;document
</ExcludeFoldersFromDeployment>
<ExcludeFilesFromDeployment>
  mystyle.css
</ExcludeFilesFromDeployment>

Where each element includes a semi-colon separated list of folder or file names (respectively). Wildcards are supported.

Metchnikoff answered 18/12, 2015 at 9:49 Comment(2)
This does not quite work it seems with the new ASP.NET 5, I did this: <ExcludeFilesFromDeployment> gulpfile.js </ExcludeFilesFromDeployment>, but the file was still copied.Simulated
@IlyaChernomordik: I've posted an issue on github about that => github.com/aspnet/vsweb-publish/issues/73Juta
F
0

I discovered that @Shane Neuville's comment above is accurate. The project.json file structure has changed and now, instead of using "publishExclude", we should use "exclude"... Here is a link to a document explaining the change in addition to what my project.json file section now looks like:

{
     "publishOptions": {
         "include": [
             "wwwroot",
             "**/*.cshtml",
             "appsettings.json",
             "appsettings.*.json",
             "web.config"
         ],
        "exclude": [
             "**/node_modules"
        ]
    }
}
Furthermore answered 19/5, 2017 at 15:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.