Skip to content

A simple library for creating sitemap files inside ASP.NET MVC applications

License

Notifications You must be signed in to change notification settings

karlosRivera/SimpleMvcSitemap

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SimpleMvcSitemap

A simple library for creating sitemap files inside ASP.NET MVC applications.

Build status Coverage Status GitHub license NuGet version

SimpleMvcSitemap lets you create sitemap files inside action methods without any configuration. It also supports generating sitemap index files. Since you are using regular action methods you can take advantage of ASP.NET MVC caching and routing.

##Table of contents

Install the NuGet package on your ASP.NET MVC project. It supports ASP.NET MVC 3/4/5 and .NET 4.0/4.5/4.5.1 versions.

Install-Package SimpleMvcSitemap

SimpleMvcSitemap references the ASP.NET MVC assembly in the earliest package. Since it's a strongly-named assembly, you will have to keep assembly binding redirection in Web.config if you are working with ASP.NET MVC 4/5. These sections are created for you in project templates.

<runtime>
  <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
      <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
      <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
    </dependentAssembly>
  </assemblyBinding>
</runtime>

You can use SitemapProvider class to create sitemap files inside any action method. You don't even have to provide absolute URLs, SimpleMvcSitemap can generate them from relative URLs. Here's an example:

public class SitemapController : Controller
{
    public ActionResult Index()
    {
        List<SitemapNode> nodes = new List<SitemapNode>
        {
            new SitemapNode(Url.Action("Index","Home")),
            new SitemapNode(Url.Action("About","Home")),
            //other nodes
        };

        return new SitemapProvider().CreateSitemap(HttpContext, nodes);
    }
}

SitemapNode class also lets you specify the optional attributes:

new SitemapNode(Url.Action("Index", "Home"))
{
    ChangeFrequency = ChangeFrequency.Weekly,
    LastModificationDate = DateTime.UtcNow,
    Priority = 0.8M
}

Sitemap files must have no more than 50,000 URLs and must be no larger then 10MB as stated in the protocol. If you think your sitemap file can exceed these limits you should create a sitemap index file. A regular sitemap will be created if you don't have more nodes than sitemap size.

SimpleMvcSitemap assumes you will get this amount of data from a data source. If you are using a LINQ provider, SimpleMvcSitemap can handle the paging.

Generating sitemap index files

This requires a little configuration:

public class ProductSitemapConfiguration : ISitemapConfiguration<Product>
{
    private readonly UrlHelper _urlHelper;

    public ProductSitemapConfiguration(UrlHelper urlHelper, int? currentPage)
    {
        _urlHelper = urlHelper;
        Size = 50000;
        CurrentPage = currentPage;
    }

    public int? CurrentPage { get; private set; }

    public int Size { get; private set; }

    public string CreateSitemapUrl(int currentPage)
    {
        return _urlHelper.RouteUrl("ProductSitemap", new { currentPage });
    }

    public SitemapNode CreateNode(Product source)
    {
        return new SitemapNode(_urlHelper.RouteUrl("Product", new { id = source.Id }));
    }
}

Then you can create the sitemap file or the index file within a single action method.

public ActionResult Products(int? currentPage)
{
    IQueryable<Product> dataSource = _products.Where(item => item.Status == ProductStatus.Active);
    ProductSitemapConfiguration configuration = new ProductSitemapConfiguration(Url, currentPage);

    return new SitemapProvider().CreateSitemap(HttpContext, dataSource, configuration);
}

You can also create index files by providing sitemap file URLs manually.

List<SitemapIndexNode> sitemapIndexNodes = new List<SitemapIndexNode>
{
    new SitemapIndexNode(Url.Action("Categories","Sitemap")),
    new SitemapIndexNode(Url.Action("Products","Sitemap"))
};

return new SitemapProvider().CreateSitemap(HttpContext, sitemapIndexNodes);

You can use Google's sitemap extensions to provide detailed information about specific content types like images, videos, mobile or news. You can still use relative URLs for any of the additional URLs.

new SitemapNode(Url.Action("Display", "Product"))
{
    Images = new List<SitemapImage>
    {
        new SitemapImage(Url.Action("Image","Product", new {id = 1})),
        new SitemapImage(Url.Action("Image","Product", new {id = 2}))
    }
};
SitemapNode sitemapNode = new SitemapNode("http://www.example.com/videos/some_video_landing_page.html")
{
    Video = new SitemapVideo(title: "Grilling steaks for summer",
                             description: "Alkis shows you how to get perfectly done steaks every time",
                             thumbnailUrl: "http://www.example.com/thumbs/123.jpg", 
                             contentUrl: "http://www.example.com/video123.flv")
};
SitemapNode sitemapNode = new SitemapNode("http://www.example.org/business/article55.html")
{
    News = new SitemapNews(newsPublication: new NewsPublication(name: "The Example Times", language: "en"),
                           publicationDate: new DateTime(2014, 11, 5, 0, 0, 0, DateTimeKind.Utc),
                           title: "Companies A, B in Merger Talks")
};
SitemapNode sitemapNode = new SitemapNode("http://mobile.example.com/article100.html")
{
    Mobile = new SitemapMobile()
};

SitemapProvider class implements the ISitemapProvider interface which can be injected to your controllers and be replaced with test doubles. All methods are thread safe so they can be used with singleton life cycle.

public class SitemapController : Controller
{
    private readonly ISitemapProvider _sitemapProvider;

    public SitemapController(ISitemapProvider sitemapProvider)
    {
        _sitemapProvider = sitemapProvider;
    }
	
    //action methods
}

SimpleMvcSitemap is licensed under MIT License. Refer to license file for more information.

About

A simple library for creating sitemap files inside ASP.NET MVC applications

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C# 99.9%
  • Other 0.1%