导航

Asp.net控件使用(1)-自定义SitemapProvider(转帖)

Posted on 2009-04-15 21:46  Brian Wang  阅读(570)  评论(0编辑  收藏  举报
The sitemap provider of .NET 2.0 is great but what if you have database generated pages ?

1 way to do it is to create a table in your database that has the same structure as the web.sitemap. And write your own custom sitemap provider.

Jef Prosise wrote an article about this in the wicked code magazine from Microsoft.

http://msdn.microsoft.com/msdnmag/issues/05/06/WickedCode/

This is all cool and nice but still I would be inputting a lot of code and every time when a database entry would change I would have to change it manually in my sitemap table.

I want to get everything directly from my different tables. The code that I provide below is not the fastest available but it is for a small application so I can’t care less. Instead of the SqlDataSource I could have used the sqlcommand and datareader to get the info out of my database which would make it faster.

Seen as it looked fairly simple to write my own sitemap provider I got to work and about 2 hours later it was all done.  I left the roles in there but for the moment the app is not using roles so I just bypass it by setting it default to “*“.

Jeff uses a dictionary to look for duplicate keys. This didn’t work so good for me but I let it be there anyway.

It comes down to it that you inherit your class from the StaticSiteMapProvider and you implement the class in your document.  For comments in the code please check jeff’s article he does it prettey good ;)

The Sitemapprovider :

 

Code

 

The web.config :

 

In the system.web add :

    <siteMap defaultProvider="MySiteMapProvider" enabled="true">

<providers>

         <clear />

         <add name="MySiteMapProvider" type="MySitemapProvider" securityTrimmingEnabled="true" connectionStringName="RTDConnectionString" />

         </providers>

     </siteMap>