0
我爲我的個人檔案類添加了一個新的布爾屬性。如何爲ASP.NET中現有用戶的新配置文件屬性設置默認值?
我似乎無法找到一種方法,但默認情況下它的值爲true。
Profile.ShowDocumentsNotApplicable
返回false時,沒有明確設置爲true ...
web.config文件內容:
<!-- snip -->
<profile inherits="Company.Product.CustomerProfile">
<providers>
<clear />
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
</profile>
<!-- snap -->
CustomerProfile:
public class CustomerProfile: ProfileBase
{
private bool _showDocumentsNotApplicable = true;
public bool ShowDocumentsNotApplicable
{
get { return Return("ShowDocumentsNotApplicable", _showDocumentsNotApplicable); }
set { Set("ShowDocumentsNotApplicable", value,() => _showDocumentsNotApplicable = value); }
}
private T Return<T>(string propertyName, T defaultValue)
{
try
{
return (T)base[propertyName];
}
catch (SettingsPropertyNotFoundException)
{
return defaultValue;
}
}
private void Set<T>(string propertyName, T setValue, System.Action defaultAction)
{
try
{
base[propertyName] = setValue;
}
catch (SettingsPropertyNotFoundException)
{
defaultAction();
}
}
}