Logo

Maarten Balliauw {blog}

ASP.NET, ASP.NET MVC, Windows Azure, PHP, ...

About the author

Maarten Balliauw is currently employed as a Technical Evangelist at JetBrains. His interests are mainly web applications developed in ASP.NET (C#) or PHP and the Windows Azure cloud platform.
More about me More about me
Send mail E-mail me


ASP.NET MVC Quickly Pro NuGet Subscribe to my RSS feed Follow me on Twitter! View Maarten Balliauw's profile on LinkedIn
Maarten Balliauw - MVP - Most Valuable Professional
Maarten Balliauw - ASPInsider

Search

Archive

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright Maarten Balliauw 2013


ASP.NET MVC MvcSiteMapProvider 1.0 released

image Back in March, I blogged about an experimental MvcSiteMap provider I was building. Today, I am proud to announce that it is stable enough to call it version 1.0! Download MvcSiteMapProvider 1.0 over at CodePlex.

Ever since the source code release I did back in March, a lot of new features have been added, such as HtmlHelper extension methods, attributes, dynamic parameters, … I’ll leave most of them up to you to discover, but there are some I want to quickly highlight.

kick it on DotNetKicks.com

ACL module extensibility

By default, MvcSiteMap will make nodes visible or invisible based on [Authorize] attributes that are placed on controllers or action methods. If you have implemented your own authentication mechanism, this may no longer be the best way to show or hide sitemap nodes. By implementing and registering the IMvcSiteMapAclModule interface, you can now plug in your own visibility logic.

public interface IMvcSiteMapAclModule
{
    /// <summary>
    /// Determine if a node is accessible for a user
    /// </summary>
    /// <param name="provider">The MvcSiteMapProvider requesting the current method</param>
    /// <param name="context">Current HttpContext</param>
    /// <param name="node">SiteMap node</param>
    /// <returns>True/false if the node is accessible</returns>

    bool IsAccessibleToUser(MvcSiteMapProvider provider, HttpContext context, SiteMapNode node);
}

Dynamic parameters

Quite often, action methods have parameters that are not really bound to a sitemap node. For instance, take a paging parameter. You may ignore this one safely when determining the active sitemap node: /Products/List?page=1 and /Products/List?page=2 should both have the same menu item highlighted. This is where dynamic parameters come in handy: MvcSiteMap will completely ignore the specified parameters when determining the current node.

<mvcSiteMapNode title="Products" controller="Products" action="List" isDynamic="true" dynamicParameters="page" />

The above sitemap node will always be highlighted, whatever the value of “page” is.

SiteMapTitle action filter attribute

In some situations, you may want to dynamically change the SiteMap.CurrentNode.Title in an action method. This can be done manually by setting  SiteMap.CurrentNode.Title, or by adding the SiteMapTitle action filter attribute.

Imagine you are building a blog and want to use the Blog’s Headline property as the site map node title. You can use the following snippet:

[SiteMapTitle("Headline")]
public ViewResult Show(int blogId) {
   var blog = _repository.Find(blogIdId);
   return blog;
}

You can also use a non-strong typed ViewData value as the site map node title:

[SiteMapTitle("SomeKey")]
public ViewResult Show(int blogId) {
   ViewData["SomeKey"] = "This will be the title";

   var blog = _repository.Find(blogIdId);
   return blog;
}

HtmlHelper extension methods

MvcSiteMap provides different HtmlHelper extension methods which you can use to generate SiteMap-specific HTML code on your ASP.NET MVC views. Here's a list of available HtmlHelper extension methods.

  • HtmlHelper.Menu() - Can be used to generate a menu
  • HtmlHelper.SiteMap() - Can be used to generate a list of all pages in your sitemap
  • HtmlHelper.SiteMapPath() - Can be used to generate a so-called "breadcrumb trail"

The MvcSiteMap release can be found on CodePlex.

kick it on DotNetKicks.com


Categories: ASP.NET | C# | General | MVC | Projects

Comments (5) -

Elijah Manor United States |

Friday, September 04, 2009 2:44 PM

Elijah Manor

Were you able to support multiple dynamic levels for the menu (example: maximumDynamicDisplayLevels)?

F.Y.I. This will be tech tweeted at 11:00AM CST

maartenba Belgium |

Thursday, June 17, 2010 2:15 PM

maartenba

Ah no, forgot that one. Can you add it as a feature request again?

Maarten Belgium |

Friday, September 04, 2009 2:55 PM

Maarten

If you mean this one: mvcsitemap.codeplex.com/.../View.aspx, yes it is in Smile

Kian Ryan United Kingdom |

Saturday, September 05, 2009 11:09 AM

Kian Ryan

I've been using this since the early days of its release for a couple of projects.  It's been a bit of a godsend, and patches a hole that frankly, shouldn't have been there.  I'm glad it's made it to 1.0.

Awesome.

Jack United States |

Monday, September 21, 2009 11:11 AM

Jack

Great, it will save us lots of time in asp.net MVC

Pingbacks and trackbacks (7)+

Comments are closed