请帮忙!如何为ASP.NET配置文件指定动态属性
我的要求是创建一个可以使用标准ASP.NET配置文件提供程序的框架。该应用程序包含两个库,一个用于概要文件访问(使用ProfileBase类保存和检索概要文件)-框架类库,另一个用于定义概要文件的属性-使用上述框架类库的开发人员客户端类库 这里的想法是,开发人员不需要知道底层概要文件的实现(在框架类库中),他们所要做的就是在类中提供属性,以便可以按预期设置和获取概要文件 我的实现如下。 (请注意,我已成功配置身份验证、连接字符串和角色提供程序) Web.config->请帮忙!如何为ASP.NET配置文件指定动态属性,asp.net,c#-3.0,profile,Asp.net,C# 3.0,Profile,我的要求是创建一个可以使用标准ASP.NET配置文件提供程序的框架。该应用程序包含两个库,一个用于概要文件访问(使用ProfileBase类保存和检索概要文件)-框架类库,另一个用于定义概要文件的属性-使用上述框架类库的开发人员客户端类库 这里的想法是,开发人员不需要知道底层概要文件的实现(在框架类库中),他们所要做的就是在类中提供属性,以便可以按预期设置和获取概要文件 我的实现如下。 (请注意,我已成功配置身份验证、连接字符串和角色提供程序) Web.config-> <profi
<profile inherits="MyCompany.FrameworkLib.ProfileSettingsService, MyCompany.FrameworkLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=12ac5ebb7ed144" >
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
</profile>
*Our design is not to specify profile properties in the web.config.
在MyCompany.FrameworkLib.profilesettings服务中
我需要能够在ProfileSettingsService中动态发现属性,而无需显式指定属性。这样,开发人员就不必担心维护两个库中的属性(一个在frameworkLib中,另一个在Consumerlib中)
非常感谢任何想法。我决定采取另一种方法来解决这个问题。不幸的是,MS设计ProfileBase类的方式并不容易做到这一点 有两种可能的解决方案。我在“b”下面使用 A.将配置文件属性添加到Web.config并使用T4模板动态生成它们。
B看一看这张照片。这也将在编译时动态生成配置文件属性。我决定采用另一种方法来解决这个问题。不幸的是,MS设计ProfileBase类的方式并不容易做到这一点 有两种可能的解决方案。我在“b”下面使用 A.将配置文件属性添加到Web.config并使用T4模板动态生成它们。
B看一看这张照片。这还将在编译时动态生成配置文件属性。访问本文,可以帮助您: 在MSDN中也显示此页面
访问本文,可以帮助您: 在MSDN中也显示此页面
public class ProfileSettingsService : ProfileBase, IProfileSettingsService
{
"**Note that if I un-comment the below code Profile works as expected. I do not want this property to be here, but somehow discover it dynamically based on the values being passed to this class. How can I do this?.**"
//[SettingsAllowAnonymous(false)]
//public string HideTransactionButton
//{
// get { return base["HideTransactionButton"] as string; }
// set { base["HideTransactionButton"] = value; }
//}
#region IProfileSettingsService Members
public T Get<T>(string key, T defaultValue)
{
if (string.IsNullOrEmpty(key))
{
throw new ArgumentNullException("key");
}
object profileValue = null;
try
{
profileValue = GetUserProfile().GetPropertyValue(key);
}
catch { }
if (profileValue is T)
{
return (T)profileValue;
}
return defaultValue;
}
public void Set<T>(string key, T value)
{
GetUserProfile().SetPropertyValue(key, value);
GetUserProfile().Save();
}
public ProfileSettingsService GetUserProfile(string username)
{
return Create(username) as ProfileSettingsService;
}
public ProfileSettingsService GetUserProfile()
{
var userName = HttpContext.User.Identity.Name;
if (userName != null)
{
return Create(userName) as ProfileSettingsService;
}
return null;
}
#endregion
}
public class ProfileContext : IProfileContext
{
#region IProfileContext Members
[Dependency] //Note that I use Unity for DI
public IProfileSettingsService ProfileSettingsService { get; set; }
public string HideTransactionButton
{
get { return this.ProfileSettingsService.Get<string>("HideTransactionButton", "false"); }
set { this.ProfileSettingsService.Set("HideTransactionButton", value); }
}
#endregion
}
//[SettingsAllowAnonymous(false)]
//public string HideTransactionButton
//{
// get { return base["HideTransactionButton"] as string; }
// set { base["HideTransactionButton"] = value; }
//}