Q:

Avoiding duplicated values in Web.config

<appSettings>
    <add key="ApiKey" value="xyz"/>
</appSettings>

<system.net>
  <mailSettings>
    <smtp deliveryMethod="Network">
      <network host="smtp.cloudmailapp.com" port="587" userName="user@gmail.com" password="xyz" />
    </smtp>
  </mailSettings>
</system.net>

As you can see, the API key "xyz" appears twice, hardcoded.
Can I use code here? I'd like to be able to call  
WebConfigurationManager.AppSettings["ApiKey"]

within the Web.config file, or something similar.

A:

I think the short answer to your question is no.  The config files cannot include C# code like you are trying to add out of the box.  
I did find another post that asks a question very similar to this. Its from a few years back, but there is a lot of feedback in it that looks like it may apply here.  Rather than trying to summarize the top answer, I will just add a link. Variables within app.config/web.config

