There is a C # ASP.NET MVC project, which, among other things, uses, for example, a logo (.png) and a style file (.css). These files must be different for different builds. How can I automate file replacement when building a project? Is it possible to specify this in the build profile or somewhere in the settings of the solution (for different assemblies there will be a solution). Thank you in advance

  • one
    What version of VS? Most likely this is solved by editing .csproj and adding conditions like Condition = "'$ (Configuration)' == 'Debug'" - koks_rs
  • @koks_rs VS2013 . In your example, is Debug the profile name? Ie, can I replace Debug with ReleaseMyProfile1 ? Yes, I thought about editing the project build script, but I was hoping for more beautiful and built-in stuff) - Andrew Zakharov
  • Debug is the value stored in the Configuration variable. - koks_rs

1 answer 1

Something similar is available in C # projects (not just MVC). For example, you can create this section in the configuration file:

<appSettings> <add key="UseTestImage" value="false" /> </appSettings> 

Then add the transformation of the configuration file: right-click on the configuration file and select the Add Config Transform option .

enter image description here

And in it we write something else, different from the default file:

  <appSettings> <add key="UseTestImage" value="true" /> </appSettings> 

And already somewhere in your code you can refer to the hidden key:

 WebConfigurationManager.AppSettings["UseTestImage"] 

and depending on the condition, do what you need. So, in theory, you can automate something. I actively use configuration file transformations:

enter image description here

You can also use the following entries in the code:

 #if DEBUG #else #endif 

Here is a good example: Various application configuration options

Exclude Files from Build: Excluding Files and Folders from Deployment

  • I just stumbled upon an article about the transformation of Web.Config , but after it, the question was still open whether it was possible to create a transformation of a custom profile, or is it only for standard Debbug and Release . But I still do not understand: here I am using the logo.png logo from the logo folder. I have two such files in the folders logo1 and logo2 . Depending on the profile, I need to take a picture from the corresponding folder, while not dragging another folder behind me. Climb into the project build script and set up copying from the folder specified in the Web.Config in it ? - Andrew Zakharov
  • @AndrewZakharov, both folders will be tightened, since they are registered in the project file. If you want to know on the presentation what image to use, you can do it like this: @if (System.Configuration.ConfigurationManager.AppSettings["UseTestImage"] == "true")... - Denis Bubnov
  • @AndrewZakharov, about setting up a copy from a folder - here I don’t even know how to help you. The project is a single whole, I do not think that it is possible to simply take and discard part of the project due to the fact that the configuration has been changed. Only if you control it manually - but this is not very good. - Denis Bubnov
  • Honestly not the most beautiful solution because of the pull-up of all folders. I'll try to leave the path logo / logo.png in the code, and in the build script copy the contents of the folder logo1 or logo2 to logo . And you can drop them by setting the Build Action property to none on each file. Sorry, you can't use this folder - Andrew Zakharov
  • 2
    To exclude files / folders from the build, the ExcludeFoldersFromDeployment and ExcludeFilesFromDeployment parameters are added in the ExcludeFoldersFromDeployment ExcludeFilesFromDeployment for more details here - qzavyer