Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
263 views
in Technique[技术] by (71.8m points)

C# Roles.GetAllRoles() error - does not contain a definition

I am trying to get all Roles in my C# application.

When I copy the C# code from: https://docs.microsoft.com/en-us/dotnet/api/system.web.security.roles.getallroles?view=netframework-4.8 into a single aspx page exactly as they have the example then the page works and I can see the Roles and add one properly.

When I have the following code-behind code:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

namespace ToolingPMv2.Admin.Access
{
    public partial class Roles : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string[] rolesArray;
                rolesArray = Roles.GetAllRoles();
                RolesGrid.DataSource = rolesArray;
                RolesGrid.DataBind();
            }

        }
    }
}

My aspx page:

<%@ Page Language="C#" AutoEventWireup="true"  MasterPageFile="~/Site.Master" CodeBehind="UserRoles.aspx.cs" Inherits="ToolingPMv2.Admin.Access.Roles" %>

<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <h2>Roles</h2>
 
     <br />

    <asp:GridView runat="server" CellPadding="2" id="RolesGrid" Gridlines="Both" CellSpacing="2" AutoGenerateColumns="false" >
        <HeaderStyle BackColor="navy" ForeColor="white" />
        <Columns>
            <asp:TemplateField HeaderText="Roles" >
                <ItemTemplate>
                    <%# Container.DataItem.ToString() %>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
       </asp:GridView>
</asp:Content>

I get the following error: 'ToolingPMv2.Admin.Access.Roles' does not contain a definition for 'GetAllRoles'

I thought that if I have 'Using System.Web.Security' I could access this method.

What am I doing wrong? This seems like a basic fundamental lesson that I should know by now.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The class for your page is named Roles so that is hiding the one in the other namespace. You need to be explicit, for example:

rolesArray = System.Web.Security.Roles.GetAllRoles();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...