我的web.config中具有以下设置键,可用于本地开发:
<add key="sagepay.kit.siteFqdn.TEST" value="https://cc2.test-domain.co.uk" />
但是我们其他开发人员中的一位决定将其设置为cc1
而不是cc2
并继续检查它。是否有一种方法可以在iis中覆盖此键,所以我不必记住要更改它每次我拉最新消息都返回吗?
或者是转换依赖于计算机名称还是类似的名称?
您可以使用以下代码段在IIS上修改web.config
文件。请注意,您需要授予用户适当的权限才能访问该文件。您可以创建一个方法并在浏览器中调用它,例如:www.yourdomain.com/Home/ReplaceKey
:
public ActionResult ReplaceKey()
{
StandardResponse response = new StandardResponse();
try
{
string appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
string configFile = System.IO.Path.Combine(appPath, "Web.config");
var currentVersion = ConfigurationManager.AppSettings["sagepay.kit.siteFqdn.TEST"];
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = AppDomain.CurrentDomain.SetupInformation.ConfigurationFile;
System.Configuration.Configuration config = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);
config.AppSettings.Settings["sagepay.kit.siteFqdn.TEST"].Value = "https://cc2.test-domain.co.uk";
config.Save();
ConfigurationManager.RefreshSection("appSettings");
response.success = true;
response.message = "success";
//response.value = ConfigurationManager.AppSettings["sagepay.kit.siteFqdn.TEST"];
}
catch(Exception ex)
{
response.success = false;
response.message = "failure";
}
return Json(response, JsonRequestBehavior.AllowGet);
}
方法响应模型:
public class StandardResponse
{
public string message { get; set; }
public bool success { get; set; }
public string value { get; set; }
}
注意:此答案用于替代IIS上的设置aka web.config文件中的任何键。我希望这会有所帮助。