/
SitemapService.cs
132 lines (113 loc) · 5 KB
/
SitemapService.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
namespace MvcBoilerplate.Services
{
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using Boilerplate.Web.Mvc;
using Boilerplate.Web.Mvc.Sitemap;
using MvcBoilerplate.Constants;
/// <summary>
/// Generates sitemap XML for the current site.
/// </summary>
public class SitemapService : SitemapGenerator, ISitemapService
{
#region Fields
private readonly ICacheService cacheService;
private readonly ILoggingService loggingService;
private readonly UrlHelper urlHelper;
#endregion
#region Constructors
/// <summary>
/// Initializes a new instance of the <see cref="SitemapService" /> class.
/// </summary>
/// <param name="cacheService">The cache service.</param>
/// <param name="loggingService">The logging service.</param>
/// <param name="urlHelper">The URL helper.</param>
public SitemapService(
ICacheService cacheService,
ILoggingService loggingService,
UrlHelper urlHelper)
{
this.cacheService = cacheService;
this.loggingService = loggingService;
this.urlHelper = urlHelper;
}
#endregion
#region Public Methods
/// <summary>
/// Gets the sitemap XML for the current site. If an index of null is passed and there are more than 25,000
/// sitemap nodes, a sitemap index file is returned (A sitemap index file contains links to other sitemap files
/// and is a way of splitting up your sitemap into separate files). If an index is specified, a standard
/// sitemap is returned for the specified index parameter. See http://www.sitemaps.org/protocol.html
/// </summary>
/// <param name="index">The index of the sitemap to retrieve. <c>null</c> if you want to retrieve the root
/// sitemap or sitemap index document, depending on the number of sitemap nodes.</param>
/// <returns>The sitemap XML for the current site or <c>null</c> if the sitemap index is out of range.</returns>
public string GetSitemapXml(int? index = null)
{
// Here we are caching the entire set of sitemap documents. We cannot use OutputCacheAttribute because
// cache expiry could get out of sync if the number of sitemaps changes.
List<string> sitemapDocuments = this.cacheService.GetOrAdd(
CacheSetting.SitemapNodes.Key,
() =>
{
IReadOnlyCollection<SitemapNode> sitemapNodes = this.GetSitemapNodes();
return this.GetSitemapDocuments(sitemapNodes);
},
CacheSetting.SitemapNodes.SlidingExpiration);
if (index.HasValue && ((index < 1) || (index.Value >= sitemapDocuments.Count)))
{
return null;
}
return sitemapDocuments[index.HasValue ? index.Value : 0];
}
#endregion
#region Protected Methods
/// <summary>
/// Gets a collection of sitemap nodes for the current site.
/// TODO: Add code here to create nodes to all your important sitemap URL's.
/// You may want to do this from a database or in code.
/// </summary>
/// <returns>A collection of sitemap nodes for the current site.</returns>
protected virtual IReadOnlyCollection<SitemapNode> GetSitemapNodes()
{
List<SitemapNode> nodes = new List<SitemapNode>();
nodes.Add(
new SitemapNode(this.urlHelper.AbsoluteRouteUrl(HomeControllerRoute.GetIndex))
{
Priority = 1
});
nodes.Add(
new SitemapNode(this.urlHelper.AbsoluteRouteUrl(HomeControllerRoute.GetAbout))
{
Priority = 2
});
nodes.Add(
new SitemapNode(this.urlHelper.AbsoluteRouteUrl(HomeControllerRoute.GetContact))
{
Priority = 2
});
// An example of how to add many pages into your sitemap.
// foreach (Product product in myProductRepository.GetProducts())
// {
// nodes.Add(
// new SitemapNode(this.urlHelper.AbsoluteRouteUrl(ProductControllerRoute.GetProduct, new { id = product.ProductId }))
// {
// Frequency = SitemapFrequency.Weekly,
// LastModified = DateTime.Now,
// Priority = 2
// });
// }
return nodes;
}
protected override string GetSitemapUrl(int index)
{
return this.urlHelper.AbsoluteRouteUrl(HomeControllerRoute.GetSitemapXml).TrimEnd('/') + "?index=" + index;
}
protected override void LogWarning(Exception exception)
{
this.loggingService.Log(exception);
}
#endregion
}
}