1

Context

I regularly make the mistake to forget include a vendor .css or .js to the Asp Mvc project. I just copy/download with a tool, or from a theme, and referencing them. All works locally because the files are in the virtual directory so IIS Express will server them.

When publish times come, and I publish the new version, those files which are not in the .csproj will not be deployed.

Question

Although some tools or itself the IDE creates warning in some cases if in a syntax construct I refer to a resource what is not in the .csproj, this is not all working (for example: when using BundleConfig) It seems to be pretty simple prevent this source of errors: Just check the file system with a well picked filter and list all files what are not included in the .csproj. (the filter could be: (*.css, .js, ...) or (assets/.*)

How can I accomplish this task?

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
g.pickardou
  • 32,346
  • 36
  • 123
  • 268
  • You can just do cross-verify in the solution explorer. It lists down only the solution mapped files by default – Prajwal Feb 21 '18 at 09:08
  • well, there are hundreds... in zillion folders. I use currently a better approach which is still not efficient enough: I turn on "show all files" in solution explorer, so I will see what is not included. Still takes minutes to open all folders, subfolders. If there was an inverse filter, like show only not included, that would be perfect – g.pickardou Feb 21 '18 at 09:20
  • which version you are using? VS 2015? – Prajwal Feb 21 '18 at 09:25
  • I am using VS 2017 – g.pickardou Feb 21 '18 at 09:27
  • you'd have to write some sort of script which parses the csproj file (it's XML)and reads what files are included in the project, and then reads the relevant folders, gets a list of files and compares them to the project list. You can do that in a c# console app maybe. – ADyson Feb 21 '18 at 10:16
  • in future try to use nuget packages to install extras into your app wherever possible, then the right files always get added to your project automatically. Or just take a bit more care when adding things! – ADyson Feb 21 '18 at 10:17

1 Answers1

3

If you switch to the new .csproj format supported by Visual Studio 2017, you no longer need to add references to files in the file system, they are picked up by default and you have to exclude files that you don't want.

Migration to the new .csproj format is pretty straightforward - you can use the dotnet migrate tool to make the conversion.

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFrameworks>net47</TargetFrameworks>
  </PropertyGroup>

  <ItemGroup>
    <ProjectReference Include="..\MyProj\MyProj.csproj" />
  </ItemGroup>

  <ItemGroup>

    <!-- /* Exclude files you don't want */ -->
    <Compile Remove="Text\AnyTransliterator.cs" />
    <Compile Remove="Text\BreakTransliterator.cs" />
  </ItemGroup>

</Project>

If you have files outside of your project directory that you want to include, you can create a link to a file or directory.

<!-- /* Link to an individual file outside of the project */ -->
<Content Include="..\..\..\Assets\something.css" Link="Assets\something.css" />

<!-- /* Create a virtual directory in Visual Studio named Assets
        and link to external Assets directory. All files in that
        directory will be included in the project */ -->
<Content Include="..\..\..\Assets\**\*" LinkBase="Assets" />

<!-- /* Create a virtual directory in Visual Studio named Assets
        and link to external Assets directory. Only .css files in that
        directory will be included in the project */ -->
<Content Include="..\..\..\Assets\**\*.css" LinkBase="Assets" />

This works with .NET Framework, but do note that you need to install the .NET Core SDK 2.0.0 in addition to VS 2017 15.3 (and ensure no global.json selects a lower SDK version) for the LinkBase option to work.

Reference: New .csproj format - How to specify entire directory as "linked file" to a subdirectory?

NightOwl888
  • 55,572
  • 24
  • 139
  • 212
  • I like this answer, but still doesn't solve OP problem, is there any tool to migrate? – Prajwal Feb 22 '18 at 07:21
  • If the files are automatically added to the `.csproj` file by default or by external reference, they will also be automatically deployed with the project. Isn't that the OP's concern? – NightOwl888 Feb 22 '18 at 07:48
  • Yea, but it isn't easy to migrate a `.csproj` file. Also, the link specifies for core projects. – Prajwal Feb 22 '18 at 07:52
  • 1
    No, the link specifies that you need the .NET Core SDK, but it will compile .NET Framework targets - `net47`. And it is not that difficult to migrate - there is a [tool](https://learn.microsoft.com/en-us/dotnet/core/migration/#dotnet-migrate) to automate most of it. – NightOwl888 Feb 22 '18 at 07:54
  • Thanks, That's what I wanted to know. – Prajwal Feb 22 '18 at 07:55
  • I'll add it to the answer. – NightOwl888 Feb 22 '18 at 07:56