C# 重写角色提供程序-ASP.NET
编辑:通过重写RoleProvider,我们希望在新方法中仍然使用相同的方法。例如Roles.GetRolesForUser(int) 上面的代码似乎有点过分 我有一个类重写RoleProvider的一些成员并添加新方法。例如C# 重写角色提供程序-ASP.NET,c#,asp.net,asp.net-membership,C#,Asp.net,Asp.net Membership,编辑:通过重写RoleProvider,我们希望在新方法中仍然使用相同的方法。例如Roles.GetRolesForUser(int) 上面的代码似乎有点过分 我有一个类重写RoleProvider的一些成员并添加新方法。例如 public string[] GetRolesForUser(int UserId) { IEnumerable<Role> RoleList = _Repo.GetRolesForUser(UserId); s
public string[] GetRolesForUser(int UserId)
{
IEnumerable<Role> RoleList = _Repo.GetRolesForUser(UserId);
string[] RetVal = new string[] {};
foreach (var curRole in RoleList)
{
RetVal[RetVal.GetUpperBound(0)] = curRole.RoleName;
}
return RetVal;
}
Web.config
<roleManager enabled="true" defaultProvider="PortalRoleProvider">
<providers>
<clear/>
<add name="PortalRoleProvider" type="PortalMVC.Providers.PortalRoleProvider"/>
</providers>
</roleManager>
有什么建议吗?在web.config中,您可以指定角色提供程序的类型,您可能需要添加可以找到该类型的程序集的名称。大概是这样的:
<add name="PortalRoleProvider" type="PortalMVC.Providers.PortalRoleProvider, MyAssemblyName" />
在web.config中指定角色提供程序的类型,您可能需要添加可找到该类型的程序集的名称。大概是这样的:
<add name="PortalRoleProvider" type="PortalMVC.Providers.PortalRoleProvider, MyAssemblyName" />
您必须将RoleProvider转换为实际的对象类型(即PortalRoleProvider),才能看到额外的功能。这适用于任何扩展了标准对象但作为超类(母版页等)访问它的情况
string[]myRoles3=((PortalRoleProvider)(Roles.Provider)).GetRolesForUser(2)代码>
正如你在文章中指出的那样,上述方法是有效的。如果您需要在代码的某一部分中大量使用此方法,则可以创建具有适当类型的指针,然后从中访问方法:
PortableRoleProvider TempPointer=(PortableRoleProvider)Roles.Provider
字符串[]myRoles3=TempPointer.GetRolesForUser(2);
您必须将角色Provider转换为实际的对象类型(即PortalRoleProvider),才能查看额外的功能。这适用于任何扩展了标准对象但作为超类(母版页等)访问它的情况
string[]myRoles3=((PortalRoleProvider)(Roles.Provider)).GetRolesForUser(2)代码>
正如你在文章中指出的那样,上述方法是有效的。如果您需要在代码的某一部分中大量使用此方法,则可以创建具有适当类型的指针,然后从中访问方法:
PortableRoleProvider TempPointer=(PortableRoleProvider)Roles.Provider
字符串[]myRoles3=TempPointer.GetRolesForUser(2);
好的,这很有意义,您可以展开吗?如果我想调用GetRolesForUser(int a),我该怎么做?“string[]myRoles=new PortalRoleProvider().GetRolesForUser(2);”这似乎有点倒退,因为我们只想在当前RoleProvider上展开。更好的是,使用接口来解耦对象。好的,这很有意义,你能展开吗?如果我想调用GetRolesForUser(int a),我该怎么做?“string[]myRoles=new PortalRoleProvider().GetRolesForUser(2);”这似乎有点倒退,因为我们只想扩展当前的RoleProvider。更好的是,使用接口来解耦对象。我已经这样做了,但这对上述问题没有帮助。谢谢。我已经这样做了,但是这对上面的问题没有帮助。谢谢你。
<add name="PortalRoleProvider" type="PortalMVC.Providers.PortalRoleProvider, MyAssemblyName" />