Mdb
Mdb

Reputation: 8568

Nuget packages to add their settings in custom xml file instead of the web/app config file

With some custom logic I have added a template for my web config in my Asp Mvc 4 application. When I need to modify the web.config file I am editing the template file for it and the changes are replaced in the web config file when I run the application. The problem is that when I add a package via nuget which needs to modify the web config file I am losing those settings as everything is overriden from my template file. Is there a way to somehow force the nuget packages to modify some custom xml file instead of the original web config file. For example, to modify mytemplate.xml instead of web.config.xml.

I am very new in this area and couldn't find a solution for my problem. As far as I understand packages that need to modify the web.config file come with web.config.transform file that merges the required fields and my only solutions is to manually replace the settings added to my web config file by the package into the template file. Is there another solution ? Thanks!

Upvotes: 2

Views: 845

Answers (1)

web_bod
web_bod

Reputation: 5758

You are using your web.config file to do two different things:

  1. Static configuration for nuget packages
  2. Dynamic configuration for your site

web.config files allow you to define some of your settings in an external file and import them - e.g.

  <configuration>
    ...
    <appSettings configSource="appSettings.config" />
    ...
  </configuration>

This is appSettings.config:

  <appSettings>
    <add key="umbracoDbDSN" value="..."/>
    <add key="DefaultInstanceName" value="..."/>
    <add key="aspnet:MaxHttpCollectionKeys" value="..."/>
  </appSettings>

It's unlikely that you need to completely re-generate your web.config every build, probably only specific sections - if these don't clash with your installed packages then this approach would give you the flexibility your application needs.

OR PLAN B

packages.config lists all of the active packages in your solution

Packages store their web.config settings in a local file: /packages/package/content/Web.config.transform

Can you extend the process that builds your web.config file to:

  1. Enumerate the active packages
  2. Check for a content/Web.config.transform file
  3. Merge those files into your web.config build pipeline

I can't see any way of re-configuring nuget to push the configuration into a different file.

Upvotes: 1

Related Questions