在 ASP.NET 网站中动态添加项目到菜单

发布于 2024-12-05 12:04:20 字数 561 浏览 1 评论 0原文

<asp:Menu ID="mnu" runat="server" PathSeparator="," CssClass="menu" DynamicMenuItemStyle-CssClass="menu">
    <Items>
        <asp:MenuItem Text="home" NavigateUrl="~/Default.aspx"  />
        <asp:MenuItem Text="Aboutus" NavigateUrl="#"/>
        <asp:MenuItem Text="Support" NavigateUrl="#" />


    </Items>
</asp:Menu>

我在母版页中有此菜单,当用户登录网站时,根据用户角色,我想从服务器端将项目添加到菜单中。我该怎么办呢。

管理(添加菜单 --> 组织、消息、群组) 用户(要添加的菜单 --> 消息、组)

因为我有 6 个角色,所以每个角色都有不同的菜单项。这怎么办

<asp:Menu ID="mnu" runat="server" PathSeparator="," CssClass="menu" DynamicMenuItemStyle-CssClass="menu">
    <Items>
        <asp:MenuItem Text="home" NavigateUrl="~/Default.aspx"  />
        <asp:MenuItem Text="Aboutus" NavigateUrl="#"/>
        <asp:MenuItem Text="Support" NavigateUrl="#" />


    </Items>
</asp:Menu>

I have this menu in master page, When the user logs into the website, based on the user role I want to add items to the menu from the server side. How can I do that.

Admin(menu to add --> Organisation, Message, Group)
Users(menu to add --> Message, Group)

Since I have 6 roles I have different menu item for each role. How can this be done

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

可可 2024-12-12 12:04:20

在主控的 Page_Load 中,您可以检查用户是否处于某些角色并动态向菜单添加值:

protected void Page_Load(object sender, EventArgs e)
{
    if (User.IsInRole("admin"))
    {
        mnu.Items.Add(new MenuItem
        {
            Text = "Administer web site",
            NavigateUrl = "~/admin.aspx"
        });
    }
}

In the Page_Load of the master you could check whether the user is in some roles and dynamically add values to the menu:

protected void Page_Load(object sender, EventArgs e)
{
    if (User.IsInRole("admin"))
    {
        mnu.Items.Add(new MenuItem
        {
            Text = "Administer web site",
            NavigateUrl = "~/admin.aspx"
        });
    }
}
奈何桥上唱咆哮 2024-12-12 12:04:20

我通常会使用站点地图和安全调整。每个 siteMapNode 都有一个“roles”属性,指示哪些角色可以查看菜单中的链接。 * 用于所有角色,或者您可以输入逗号分隔的角色列表。例如

<?xml version="1.0" encoding="utf-8" ?>
<siteMap>
  <siteMapNode title="Home" description="Home" 
       url="~/default.aspx" roles="*" >
  </siteMapNode>
  <siteMapNode title="Organization" description="Organization" 
       url="~/Organization.aspx" roles="Admin" >
  </siteMapNode>
  <siteMapNode title="Message" description="Message" 
       url="~/Organization.aspx" roles="Admin, User" >
  </siteMapNode>
</siteMap>

等。

然后您可以在 web.config 中启用安全修剪:

<siteMap defaultProvider="XmlSiteMapProvider" enabled="true">
  <providers>
    <add name="XmlSiteMapProvider"
         description="Default Site Map Provider"
         type="System.Web.XmlSiteMapProvider"
         siteMapFile="Web.sitemap"
         securityTrimmingEnabled="true" />
  </providers>
</siteMap>

然后您所要做的就是将 ASP 菜单的数据源设置为站点地图。更多信息可以在这里找到:http://msdn.microsoft.com/en- us/library/305w735z.aspx 和此处:http://msdn.microsoft.com/en-us/ Library/ms178429(v=vs.80).aspx

我喜欢这种方法,因为添加基于角色的新菜单项要容易得多。您不必手动检查代码中的角色,无论如何,这可能最终会成为一个笨拙的 if 语句。

I'd generally use a site map and security trimming. Each siteMapNode has a "roles" attribute that indicates which roles are allowed to see the link in the menu. * is used for all roles or you can enter a comma separated list of roles. e.g.

<?xml version="1.0" encoding="utf-8" ?>
<siteMap>
  <siteMapNode title="Home" description="Home" 
       url="~/default.aspx" roles="*" >
  </siteMapNode>
  <siteMapNode title="Organization" description="Organization" 
       url="~/Organization.aspx" roles="Admin" >
  </siteMapNode>
  <siteMapNode title="Message" description="Message" 
       url="~/Organization.aspx" roles="Admin, User" >
  </siteMapNode>
</siteMap>

etc.

Then you can enable security trimming in your web.config:

<siteMap defaultProvider="XmlSiteMapProvider" enabled="true">
  <providers>
    <add name="XmlSiteMapProvider"
         description="Default Site Map Provider"
         type="System.Web.XmlSiteMapProvider"
         siteMapFile="Web.sitemap"
         securityTrimmingEnabled="true" />
  </providers>
</siteMap>

All you have to do then is set the datasource of your asp menu to the site map. More information can be found here: http://msdn.microsoft.com/en-us/library/305w735z.aspx and here: http://msdn.microsoft.com/en-us/library/ms178429(v=vs.80).aspx

I like this approach because adding a new role based menu item is much easier. You don't have to manually check the role in the code behind which will probably end up as an unwieldy if statement anyway.

以酷 2024-12-12 12:04:20

您可以在用户登录时创建一个Session。

Session["user"] = user;

在母版页中;

var user = Session["user"];

You can create a Session when the user login.

Session["user"] = user;

In Master Page;

var user = Session["user"];
说好的呢 2024-12-12 12:04:20

基于动态用户权限的菜单

 C# CLASS FILES
        public class url_details
        {
            public string url;
            public string page_name;
            public string icon;
        }

        C# inside login page            
        List<url_details> url_list = new List<url_details>();            
        foreach (DataRow dr in dataTable.Rows)
        {
            url_details url_item = new url_details();
            url_item.url = dr["url"].ToString();
            url_item.page_name = dr["page_name"].ToString();
            url_item.icon = dr["icon"].ToString();
            url_list.Add(url_item);
        }           
        Session["urls"] = url_list;


        C#-HTML MENU FORM
         <%
            var uruls = (List<url_details>)Session["urls"];
            foreach (var url in uruls)
            {%>
                <li><a href="..<%=url.url %>"><%=url.icon %><span><%=url.page_name %></span></a></li>

            <% }                

        %>

Dynamic User Privilege Based Menu

 C# CLASS FILES
        public class url_details
        {
            public string url;
            public string page_name;
            public string icon;
        }

        C# inside login page            
        List<url_details> url_list = new List<url_details>();            
        foreach (DataRow dr in dataTable.Rows)
        {
            url_details url_item = new url_details();
            url_item.url = dr["url"].ToString();
            url_item.page_name = dr["page_name"].ToString();
            url_item.icon = dr["icon"].ToString();
            url_list.Add(url_item);
        }           
        Session["urls"] = url_list;


        C#-HTML MENU FORM
         <%
            var uruls = (List<url_details>)Session["urls"];
            foreach (var url in uruls)
            {%>
                <li><a href="..<%=url.url %>"><%=url.icon %><span><%=url.page_name %></span></a></li>

            <% }                

        %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文