Publishing external configuration files in ASP.Net

2019-05-19 05:32发布

问题:

I have a ASP.net MVC 4 web application and web.config is referencing some other external config files (e.g. ). When publishing the website only web.config gets published and none of these external files will be deployed.

Note: I have set properties of these external config file as: BuildAction : Content, Copy always too but didn't change anything!

Has anybody come up with a solution for this?

Thanks

回答1:

Modify your project file as such:

<PropertyGroup>
  <CopyAllFilesToSingleFolderForPackageDependsOn>
    CustomConfigFiles;
    $(CopyAllFilesToSingleFolderForPackageDependsOn);
  </CopyAllFilesToSingleFolderForPackageDependsOn>

  <CopyAllFilesToSingleFolderForMsdeployDependsOn>
    CustomConfigFiles;
    $(CopyAllFilesToSingleFolderForPackageDependsOn);
  </CopyAllFilesToSingleFolderForMsdeployDependsOn>
</PropertyGroup>

<Target Name="CustomConfigFiles">
  <ItemGroup>
    <YourCustomConfigFiles Include="..\Path\To\Your\**\*.config" />

    <FilesForPackagingFromProject  Include="%(YourCustomConfigFiles)">
      <DestinationRelativePath>Target\Path\For\Your\%(RecursiveDir)%(Filename)%(Extension)</DestinationRelativePath>
    </FilesForPackagingFromProject>
  </ItemGroup>
</Target>

based on this answer