如何以编程方式配置web.安装在IIS中的网站的配置信息
本文关键字:配置 IIS 信息 网站 web 编程 方式 安装 | 更新日期: 2023-09-27 18:16:42
我已经开发了一个安装程序,将添加一个新的网站到IIS 7.5。一旦网站是在我想配置它的连接字符串创建。安装完成后,如下所示:
<connectionStrings>
<add name="DefaultConnection" connectionString="$(ReplacableToken_DefaultConnection-Web.config Connection String_0)" providerName="System.Data.SqlClient" />
<add name="WebContext" connectionString="$(ReplacableToken_WebContext-Web.config Connection String_0)" providerName="System.Data.SqlClient" />
<add name="ActivityContext" connectionString="$(ReplacableToken_ActivityContext-Web.config Connection String_0)" providerName="System.Data.SqlClient" />
</connectionStrings>
我想用c#来配置它们。下面是我的尝试:
using (ServerManager manager = new ServerManager())
{
Configuration webConfig = manager.GetWebConfiguration(myWebsite.name);
ConfigurationSection directoryBrowseSection = webConfig.GetSection("connectionStrings");
directoryBrowseSection.SetAttributeValue("DefaultConnection", ".''SQLEXPRESS;Database=_usr;Integrated Security=true");
directoryBrowseSection.SetAttributeValue("WebContext", ".''SQLEXPRESS;Database=_main;Integrated Security=true");
directoryBrowseSection.SetAttributeValue("ActivityContext", ".''SQLEXPRESS;Database=_activity;Integrated Security=true");
manager.CommitChanges();
}
得到的结果:异常详细信息:System.Runtime.InteropServices.COMException:无效索引。(Exception from HRESULT: 0x80070585).
异常在以下行:
directoryBrowseSection.SetAttributeValue("DefaultConnection", ".''SQLEXPRESS;Database=_usr;Integrated Security=true");
我的问题,如果不清楚,我如何配置使用Microsoft.web.administration在IIS上安装的网站上的连接字符串?
我知道这篇文章有点老了,但是我在寻找类似的东西时偶然发现了它。这是我想到的:
//using Microsoft.Web.Administration;
var file = "PATH TO FILE";
var configFile = new FileInfo(file);
var virtualDirectoryMapping = new VirtualDirectoryMapping(configFile.DirectoryName, true, configFile.Name);
var webConfigFileMap = new WebConfigurationFileMap();
webConfigFileMap.VirtualDirectories.Add("/", virtualDirectoryMapping);
var webConfig = System.Web.Configuration.WebConfigurationManager.OpenMappedWebConfiguration(webConfigFileMap, "/");
webConfig.ConnectionStrings.ConnectionStrings.Add(new System.Configuration.ConnectionStringSettings("NAME", "CONNECTION STRING"));
webConfig.Save();
如果这些帮助其他人需要做任何与。net配置文件(获取配置,拉appSettings或ConnectionString节):
using System.Configuration;
using System.IO;
using System.Web.Configuration;
namespace YourNamespace
{
public static class DotNetConfigFile
{
public static Configuration GetConfiguration(string filePath)
{
if (File.Exists(filePath))
{
FileInfo file = new FileInfo(filePath);
if (file.Name.ToLower() != "web.config")
{
try
{
ExeConfigurationFileMap map = new ExeConfigurationFileMap() { ExeConfigFilename = filePath };
return ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
}
catch (ConfigurationErrorsException ex)
{
return null;
//throw;
}
}
else
{
try
{
var virtualDirectoryMapping = new VirtualDirectoryMapping(file.DirectoryName, true, file.Name);
var webConfigFileMap = new WebConfigurationFileMap();
webConfigFileMap.VirtualDirectories.Add("/", virtualDirectoryMapping);
return WebConfigurationManager.OpenMappedWebConfiguration(webConfigFileMap, "/");
}
catch(ConfigurationErrorsException ex)
{
return null;
//throw;
}
}
}
else
throw new FileNotFoundException("File not found", filePath);
}
public static KeyValueConfigurationCollection GetAppSettings(string filePath)
{
var config = GetConfiguration(filePath);
if (config != null)
return config.AppSettings.Settings;
else
return null;
}
public static KeyValueConfigurationCollection GetAppSettings(FileInfo fileInfo)
{
return GetAppSettings(fileInfo.FullName);
}
public static ConnectionStringSettingsCollection GetConnectionStrings(string filePath)
{
var config = GetConfiguration(filePath);
if (config != null)
return config.ConnectionStrings.ConnectionStrings;
else
return null;
}
public static ConnectionStringSettingsCollection GetConnectionStrings(FileInfo fileInfo)
{
return GetConnectionStrings(fileInfo.FullName);
}
}
由于缺乏答案,而且我无法找到使用microsoft.web的方法。管理方面,我求助于网上阅读。从其物理路径配置。这样我就可以按照以下方式配置我需要的任何元素:
string[] file = Directory.GetFiles(myWebsite.physicalPath, "*config");
if (file != null)
{
XmlDocument doc = new XmlDocument();
doc.Load(file[1]);
XmlNodeList configurationStrings = doc.SelectNodes("/configuration/connectionStrings/add");
configurationStrings[0].Attributes["connectionString"].Value = ".''SQLEXPRESS;Database=_usr;Integrated Security=true";
configurationStrings[1].Attributes["connectionString"].Value = ".''SQLEXPRESS;Database=_main;Integrated Security=true";
configurationStrings[2].Attributes["connectionString"].Value = ".''SQLEXPRESS;Database=_activity;Integrated Security=true";
doc.Save(file[1]);
}