asp.net 2.0中根据roles显示不同的sitemap
Web.sitemap文件中siteMapNode节点有roles属性,但是直接设置并不起作用,查证资料后发现需在web.config文件中设置Site Map Providers才可以。
<system.web>
...
<siteMap defaultProvider="XmlSiteMapProvider" enabled="true">
<providers>
<add name="XmlSiteMapProvider"
description="SiteMap provider which reads in .sitemap XML files."
type="System.Web.XmlSiteMapProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
siteMapFile="Web.sitemap"
securityTrimmingEnabled="true"/>
</providers>
</siteMap>
</system.web>
...
<siteMap defaultProvider="XmlSiteMapProvider" enabled="true">
<providers>
<add name="XmlSiteMapProvider"
description="SiteMap provider which reads in .sitemap XML files."
type="System.Web.XmlSiteMapProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
siteMapFile="Web.sitemap"
securityTrimmingEnabled="true"/>
</providers>
</siteMap>
</system.web>
注意siteMap这一节, 关键是这里的设置,然后在Web.sitemap文件中相应的siteMapNode节点上加上roles属性即可。但实际应用中发现只可以加在一级节点上,二级节点上无效,例子如下。
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="" title="系统管理" description="" roles="系统管理帐户,用户管理员">
<siteMapNode url="~/System/UserManage.aspx" title="用户管理" description=""/>
<siteMapNode url="~/System/CreateUser.aspx" title="创建用户" description=""/>
<siteMapNode url="~/System/CityManage.aspx" title="地市维护" description=""/>
</siteMapNode>
</siteMap>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="" title="系统管理" description="" roles="系统管理帐户,用户管理员">
<siteMapNode url="~/System/UserManage.aspx" title="用户管理" description=""/>
<siteMapNode url="~/System/CreateUser.aspx" title="创建用户" description=""/>
<siteMapNode url="~/System/CityManage.aspx" title="地市维护" description=""/>
</siteMapNode>
</siteMap>
另外查询MSDN,发现可以使用Site Map Providers可以连接到数据库中读取sitemap
首先要使用SiteMapApi来作一个自定义的Provider
里面用到了一个存储过程
CREATE PROCEDURE proc_GetSiteMap AS SELECT [ID], [Title],[Description], [Url], [Roles], [Parent] FROM [SiteMap] ORDER BY [ID]
然后在web.config里做如下设置
<configuration>
<connectionStrings>
<add name="SiteMapConnectionString" connectionString="..." />
</connectionStrings>
<system.web>
<siteMap enabled="true" defaultProvider="AspNetSqlSiteMapProvider">
<providers>
<add name="AspNetSqlSiteMapProvider"
type="SqlSiteMapProvider, CustomProviders"
description="SQL Server site map provider"
securityTrimmingEnabled="true"
connectionStringName="SiteMapConnectionString"
/>
</providers>
</siteMap>
</system.web>
</configuration>
数据库sitemap表结构
CREATE TABLE [dbo].[SiteMap] (
[ID] [int] NOT NULL,
[Title] [varchar] (32),
[Description] [varchar] (512),
[Url] [varchar] (512),
[Roles] [varchar] (512),
[Parent] [int]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[SiteMap] ADD
CONSTRAINT [PK_SiteMap] PRIMARY KEY CLUSTERED
(
[ID]
) ON [PRIMARY]
GO
[ID] [int] NOT NULL,
[Title] [varchar] (32),
[Description] [varchar] (512),
[Url] [varchar] (512),
[Roles] [varchar] (512),
[Parent] [int]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[SiteMap] ADD
CONSTRAINT [PK_SiteMap] PRIMARY KEY CLUSTERED
(
[ID]
) ON [PRIMARY]
GO
数据的实例
MSDN上Site Map Providers说明地址:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/ASPNETProvMod_Prt3.asp
。