A web.config modification definition is expressed as a set of commands that, when processed by the web.config manipulator in Windows SharePoint Services, changes the state of web.config. You can string together a set of these commands to ensure that they apply the desired tags and attributes within web.config. Each modification is expressed as an object in the administrative object model.
Use the WebConfigModifications property of the SPWebApplication or SPWebService class to get the collection of web.config modifications either in the Web application or in all Web applications within the Web service.
Here's a console app as POC.
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
class Program {
private const string ScriptResourceHandler = @"<add verb=""GET,HEAD"" path=""FOO.bar"" type=""System.Web.Handlers.ScriptResourceHandler, System.Web.Extensions"" validate=""false""/>"
static void Main(string[] args)
{
SPSite siteCollection = new SPSite("http://localhost");
SPWebApplication webApp = siteCollection.WebApplication;
SPWebConfigModification modification = new SPWebConfigModification();
modification.Path = "configuration/system.web/httpHandlers"
modification.Name = "Example"
modification.Value = value;
modification.Owner = "ExampleOwner"
modification.Sequence = 0;
modification.Type = SPWebConfigModification.SPWebConfigModificationType.EnsureChildNode;
webApp.WebConfigModifications.Add(modification);
// .Remove doesn't remove it from the web.config, BTW... :(
webApp.Farm.Services.GetValue<SPWebService>().ApplyWebConfigModifications();
}
}