20. december 2005 - 08:16
#2
prøv med :
Imports System
Imports System.Web
Imports System.Data.SqlClient
Imports System.Collections.Specialized
Imports System.Configuration
Imports System.Web.Configuration
Imports System.Collections.Generic
Imports System.Runtime.CompilerServices
Imports System.Configuration.Provider
Imports System.Security.Permissions
Imports System.Data.Common
<SqlClientPermission (SecurityAction.Demand, Unrestricted=True)> _
Public Class SqlSiteMapProvider
Inherits StaticSiteMapProvider
private const String _errmsg1 = "Missing node ID"
private const String _errmsg2 = "Duplicate node ID"
private const String _errmsg3 = "Missing parent ID"
private const String _errmsg4 = "Invalid parent ID"
private const String _errmsg5 =
"Empty or missing connectionStringName"
private const String _errmsg6 = "Missing connection string"
private const String _errmsg7 = "Empty connection string"
Private _connect As String
private Integer _indexID, _indexTitle, _indexUrl,
_indexDesc, _indexRoles, _indexParent
private Dictionary<int, SiteMapNode> _nodes =
New Dictionary<int, SiteMapNode>(16)
Private _root As SiteMapNode
Public override void Initialize (String name,
NameValueCollection config)
{
' Verify that config isn't null
If config Is Nothing Then
Throw New ArgumentNullException("config")
End If
' Assign the provider a default name if it doesn't have one
If String.IsNullOrEmpty(name) Then
name = "SqlSiteMapProvider"
End If
' Add a default "description" attribute to config if the
' attribute doesn't exist or is empty
If String.IsNullOrEmpty(config("description")) Then
config.Remove("description")
config.Add("description", "SQL site map provider")
End If
' Call the base class's Initialize method
MyBase.Initialize(name, config)
' Initialize _connect
Dim connect As String = config("connectionStringName")
If String.IsNullOrEmpty(connect) Then
Throw New ProviderException (_errmsg5)
End If
config.Remove ("connectionStringName")
If WebConfigurationManager.ConnectionStrings(connect) Is Nothing Then
Throw New ProviderException (_errmsg6)
End If
_connect = WebConfigurationManager.ConnectionStrings
<connect).ConnectionString
If String.IsNullOrEmpty(_connect) Then
Throw New ProviderException (_errmsg7)
End If
' In beta 2, SiteMapProvider processes the
' securityTrimmingEnabled attribute but fails to remove it.
' Remove it now so we can check for unrecognized
' configuration attributes.
If Not config("securityTrimmingEnabled") Is Nothing Then
config.Remove("securityTrimmingEnabled")
End If
' Throw an exception if unrecognized attributes remain
If config.Count > 0 Then
Dim attr As String = config.GetKey(0)
If Not String.IsNullOrEmpty(attr) Then
Throw New ProviderException
("Unrecognized attribute: " + attr)
End If
End If
}
Public override SiteMapNode BuildSiteMap()
{
lock (Me)
{
' Return immediately if this method has been called before
If Not _root Is Nothing Then
Return _root
End If
' Query the database for site map nodes
Dim connection As SqlConnection = New SqlConnection(_connect)
Try
connection.Open()
SqlCommand command =
New SqlCommand("proc_GetSiteMap", connection)
command.CommandType = CommandType.StoredProcedure
Dim reader As SqlDataReader = command.ExecuteReader()
_indexID = reader.GetOrdinal("ID")
_indexUrl = reader.GetOrdinal("Url")
_indexTitle = reader.GetOrdinal("Title")
_indexDesc = reader.GetOrdinal("Description")
_indexRoles = reader.GetOrdinal("Roles")
_indexParent = reader.GetOrdinal("Parent")
If reader.Read() Then
' Create the root SiteMapNode and add it to
' the site map
_root = CreateSiteMapNodeFromDataReader(reader)
AddNode(_root, Nothing)
' Build a tree of SiteMapNodes underneath
' the root node
While reader.Read()
' Create another site map node and
' add it to the site map
SiteMapNode node =
CreateSiteMapNodeFromDataReader(reader)
AddNode(node,
GetParentNodeFromDataReader (reader))
End While
End If
Finally
connection.Close()
End Try
' Return the root SiteMapNode
Return _root
}
}
protected override SiteMapNode GetRootNodeCore ()
{
BuildSiteMap ()
Return _root
}
' Helper methods
private SiteMapNode
CreateSiteMapNodeFromDataReader (DbDataReader reader)
{
' Make sure the node ID is present
If reader.IsDBNull(_indexID) Then
Throw New ProviderException (_errmsg1)
End If
' Get the node ID from the DataReader
Dim id As Integer = reader.GetInt32(_indexID)
' Make sure the node ID is unique
If _nodes.ContainsKey(id) Then
Throw New ProviderException(_errmsg2)
End If
' Get title, URL, description, and roles from the DataReader
String title = reader.IsDBNull (_indexTitle) ?
Nothing : reader.GetString (_indexTitle).Trim ()
String url = reader.IsDBNull (_indexUrl) ?
Nothing : reader.GetString (_indexUrl).Trim ()
String description = reader.IsDBNull (_indexDesc) ?
Nothing : reader.GetString (_indexDesc).Trim ()
String roles = reader.IsDBNull(_indexRoles) ?
Nothing : reader.GetString(_indexRoles).Trim()
' If roles were specified, turn the list into a string array
Dim rolelist() As String = Nothing
If Not String.IsNullOrEmpty(roles) Then
","c, ";"c
End If
, 512)
' Create a SiteMapNode
SiteMapNode node = New SiteMapNode(this, id.ToString(), url,
title, description, rolelist, Nothing, Nothing, Nothing)
' Record the node in the _nodes dictionary
_nodes.Add(id, node)
' Return the node
Return node
}
private SiteMapNode
GetParentNodeFromDataReader(DbDataReader reader)
{
' Make sure the parent ID is present
If reader.IsDBNull(_indexParent) Then
Throw New ProviderException (_errmsg3)
End If
' Get the parent ID from the DataReader
Dim pid As Integer = reader.GetInt32(_indexParent)
' Make sure the parent ID is valid
If Not _nodes.ContainsKey(pid) Then
Throw New ProviderException(_errmsg4)
End If
' Return the parent SiteMapNode
Return _nodes(pid)
}
End Class
20. december 2005 - 08:16
#3
en 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>