Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using transformed Web.config with IIS Express during debug

I have a Visual Studio application that has multiple Solution Configurations. There is a Web.config transform file for each configuration. For example, Web.Debug.config, Web.Release.config, etc.

We also have a couple of developers working on this project that have nonstandard SQL Express instance names due to the way they installed SQL Express and rather than having them continually editing Web.Debug.config to run in their environment I have setup a Solution Configuration for each of them and added the following to the very bottom of the .csproj file. This code does work in that it triggers the creation of Web.config and MyWebApp.dll.config in the VS /obj/Debug-DeveloperName/ folder.

The transformed .config files are perfect, but IIS Express still uses the root Web.config (not transformed).

Is there a way to get IIS Express to use these transformed Web.config files while debugging locally?

<UsingTask 
    TaskName="TransformXml"
    AssemblyFile="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.Publishing.Tasks.dll" />

<Target 
    Name="AfterCompile" 
    Condition="exists('Web.$(Configuration).config')">

    <!-- Generate transformed config in intermediate directory -->
    <TransformXml 
        Source="Web.config" 
        Destination="$(IntermediateOutputPath)$(TargetFileName).config"
        Transform="Web.$(Configuration).config" 
    />
</Target>

Using the web application's Web.Debug.Config works for most of us, but not all.

There must be a way of getting IIS Express to use the transformed Web.Debug-DeveloperName.config during local debug?

Does the transformed Web.config have to be copied into a different folder?

like image 648
rwkiii Avatar asked Mar 17 '15 08:03

rwkiii


1 Answers

I faced this problem before and I found a solution. Unfortunately, the solution is not based on forcing IIS to use different name of the config, but if you follow steps below, you will just select the configuration and run you app (which is ewhat you need I think). The Web.config transform will occur before build and it will replace the original Web.config by the transformad one. Then, when the deployment (to local IIS Express) begins, it will already use the transformed one.

Here is step by step how I did this in one project (VS2012):

  1. Right click on the project and select Unload
  2. Right click on it again and select Edit
  3. Go to the bottom of the file and append the follwing to the right over the "" tag (it will be last item)
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\WebApplications\Microsoft.WebApplication.targets" />
<Target Name="BeforeBuild" Condition="'$(PublishProfileName)' == '' And '$(WebPublishProfileFile)' == ''">
    <TransformXml Source="Web.config" Transform="Web.$(Configuration).config" Destination="Web.config" />
</Target>

The condition there is to prevent duplicate transformation when publishing.

  1. Save the file, right click on it and select Reload

Now, everytime you run a build, your Web.config will be transformed according to selected configuration.

like image 96
Martin Brabec Avatar answered Sep 20 '22 06:09

Martin Brabec