FluentSitemap: Build Sitemaps for Asp.Net MVC
Building your site is only half the battle. Getting people to know it exists is the other half. If you are creating a public facing site, or even an intranet that utilizes an in-house search server then I have created a library just for you. The library is FluentSitemap. This library is designed to help you easily build sitemaps to be utilized by major search engines. The great thing about this library is that utilizes all your existing routes and controllers to build a sitemap even for the most complex Asp.Net MVC sites.
The FluentSitemap library can be found at GitHub Here.
How to use you, Let me count the ways
There are several ways you can utilize this library. The first is to specify each node manually. Let’s look at how that looks.
// You can pass in a HttpContext from anywhere
// in you application
ISitemapConfigurator configurator = new SitemapConfigurator(HttpContext);
// create sitemap node and set
ISitemap sitemap = configurator.Create()
.WithLocation("http://localhost.com/")
.WithPriority(0.3)
.WithChangeFrequency(ChangeFrequencyType.Never)
.Set().Export();
The second is to use a controller/action pair.
// You can pass in a HttpContext from anywhere
// in you application
ISitemapConfigurator configurator = new SitemapConfigurator(HttpContext);
// Add From a controller and action
ISitemap sitemap = configurator.Add("Home", "Index")
.Add("Home", "About").Export();
The third is to use a route
// You can pass in a HttpContext from anywhere
// in you application
ISitemapConfigurator configurator = new SitemapConfigurator(HttpContext);
// Add From a controller and action
ISitemap sitemap = configurator
// Add From a Route
.AddFromRoute("Default", new {id = "2"}).Export();
The last is to use the ISitemetadata. This is specifically there for IoC.
public class HomeControllerSitemapMetadata : ISitemapMetadata
{
private const string Home = "Home";
#region ISitemapMetadata Members
public void Create(ISitemapConfigurator sitemap)
{
sitemap.Add(Home, "Index")
.Add(Home, "Scanner")
.Add(c => c.Metadata());
}
#endregion
}
public class OtherControllerSitemapMetadata : ISitemapMetadata
{
private const string Other = "Other";
#region ISitemapMetadata Members
public void Create(ISitemapConfigurator sitemap)
{
sitemap.Add(Other, "Index")
.Add(c => c.Test(1, "dude"));
}
#endregion
}
// An example, you'll probably use your favorite
// IoC container to resolve all the metadata classes
var metadata = new List
{new HomeControllerSitemapMetadata(), new OtherControllerSitemapMetadata()};
ISitemap sitemap = new SitemapConfigurator(HttpContext).FromMetaData(metadata).Export();
There is more API sugar, so go download it and give it a try.
