覆盖角色提供程序 - ASP.NET
编辑:通过重写 RoleProvider,我们希望仍然在新方法中使用相同的方法。例如 Roles.GetRolesForUser(int)。
string[] myRoles3 = ((PortalRoleProvider) (Roles.Provider)).GetRolesForUser(2);
上面的代码看起来有点多余?
我有一个类,它重写 RoleProvider 的一些成员并添加新方法。例如。
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;
}
现在,当我在代码逻辑中时,我想编写 Roles.GetRolesForUser(2) 但当我键入此内容时,它似乎确实看到了我添加的额外方法。这是类声明和 webconfig。
namespace PortalMVC.Providers
{
public class PortalRoleProvider : RoleProvider
{
Web.config
<roleManager enabled="true" defaultProvider="PortalRoleProvider">
<providers>
<clear/>
<add name="PortalRoleProvider" type="PortalMVC.Providers.PortalRoleProvider"/>
</providers>
</roleManager>
有什么建议吗?
Edit: By Overriding the RoleProvider we were hoping to still use the same methods with our new methods. Such as Roles.GetRolesForUser(int).
string[] myRoles3 = ((PortalRoleProvider) (Roles.Provider)).GetRolesForUser(2);
That above code seems a bit excessive ?
I have a class which overrides some members of the RoleProvider and adds new methods. Eg.
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;
}
Now when I am in the logic of my code I want to write Roles.GetRolesForUser(2) but when i type this, it does seem to see the extra method that I have added. Here is the class declaration and the webconfig.
namespace PortalMVC.Providers
{
public class PortalRoleProvider : RoleProvider
{
Web.config
<roleManager enabled="true" defaultProvider="PortalRoleProvider">
<providers>
<clear/>
<add name="PortalRoleProvider" type="PortalMVC.Providers.PortalRoleProvider"/>
</providers>
</roleManager>
Any suggestions why this is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您必须将 RoleProvider 转换为实际对象类型(即 PortalRoleProvider)才能看到额外的功能。这适用于任何时候您扩展了标准对象,但将其作为超类(母版页等)进行访问。
string[] myRoles3 = ((PortalRoleProvider) (Roles.Provider)).GetRolesForUser(2) 。
正如您在帖子中指出的那样,上述内容是有效的 如果您需要在一段代码中大量使用它,您可以创建一个具有正确类型的指针,然后从那里访问方法:
PortableRoleProvider TempPointer=(PortableRoleProvider)Roles.Provider;
string[] myRoles3=TempPointer.GetRolesForUser(2);
You have to cast your RoleProvider as your actual object type (i.e. PortalRoleProvider) to see the extra functions. This applies to any time you've extended a standard object but are accessing it as it's super class (Master pages, etc.)
string[] myRoles3 = ((PortalRoleProvider) (Roles.Provider)).GetRolesForUser(2);
The above, as you've pointed out in your post, works. If you need to use this a lot in a section of code, you can create a pointer with the proper type then access the methods from there:
PortableRoleProvider TempPointer=(PortableRoleProvider)Roles.Provider;
string[] myRoles3=TempPointer.GetRolesForUser(2);
在 web.config 中指定角色提供程序的类型时,您可能需要添加可以找到该类型的程序集的名称。像这样的东西:
In the web.config, where you specify the type for the role provider, you might need to add the name of the assembly where the type can be found. Something like this: