![](/img/trans.png)
[英]How to fix 'Key already exists Parameter name: Key' error in UltraGrid?
[英]The settings key with code name 'XX' already exists
我试图在Kentico中保存设置,但出现此错误:
代号为“ AvalaraOrderStatus”的设置键已存在。
我已经创建了设置,并且已经保存了一个值。 该代码在Kentico 8中运行良好,但是没有要求我提供SiteInfiIdentifer。
这是我创建以进行设置的代码:
//if the setting does not exist, then create it
if (SettingsKeyInfoProvider.GetSettingsKeyInfo(siteName + ".AvalaraOrderStatus", siteID) == null)
{
// Create and set up new SettingsKey
SettingsKeyInfo si = new SettingsKeyInfo();
si.KeyName = "AvalaraOrderStatus";
si.KeyDisplayName = "Avalara Order Status";
si.KeyDescription = "Avalara order status for this site";
si.KeyType = "string";
si.KeyValue = string.Empty;
si.KeyCategoryID = category.CategoryID;
SettingsKeyInfoProvider.SetSettingsKeyInfo(si);
}
代码在最后一行抛出错误。 这是我的代码:
int currentSiteID = CMS.SiteProvider.SiteContext.CurrentSiteID;
SiteInfoIdentifier siteId = new SiteInfoIdentifier(currentSiteID);
//update settings in system
SettingsKeyInfoProvider.SetValue(siteName + ".AvalaraOrderStatus", siteId, orderStatus.Trim());
注意事项:
SettingsKeyInfoProvider.GetSettingsKeyInfo
方法的第一个参数不需要以站点名称为前缀。 这就是提供站点标识符(在您的情况下为SiteID
)的原因。 否则,每次if
语句求值时,您可能会得到一个null
值,这就是为什么即使存在该设置键也要重新创建的原因。 因此应该是: SettingsKeyInfoProvider.GetSettingsKeyInfo("AvalaraOrderStatus", siteID)
SettingsKeyInfoProvider.SetValue
方法-无需在站点名称前添加前缀: SettingsKeyInfoProvider.SetValue("AvalaraOrderStatus", siteId, orderStatus.Trim())
CurrentSiteID
整数是有效的SiteIdentifier,因此无需显式实例化SiteInfoIdentifier
对象: SettingsKeyInfoProvider.SetValue("AlavaraOrderStatus", CMS.SiteProvider.SiteContext.CurrentSiteID, orderStatus.Trim())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.