Skip to content

Commit 86df229

Browse files
committed
Merge branch 'feature/SitemapController-Extended' into develop
Simplified the output of the default sitemap; introduced an optional `/Sitemap/Extended` action for exposing all metadata. Previously, the sitemap output (most) attributes and relationships as additional metadata. Few agents support this metadata, and no current implementations utilize it. The main reason it was in place is to support the legacy Google Custom Search Engine (CSE), which used to support it—but is no longer offered by Google. As that functionality adds a lot of weight to the default sitemap, it's been removed from the default output (87b6d1b). It is still supported, however, it via the `Extended()` action (84c054f), in case future agents provide support for the `<PageMap />` and `<DataObject />` elements. In addition, a new `MapTopicSitemap()` extension method was introduced (a4b449d) to simplify configuration of the sitemap route for any sites not using e.g. the `MapDefaultControllerRoute()`, which would otherwise cover the `SitemapController`. Finally, both `MapTopicSitemap()` (a4b449d) and the preexisting `MapTopicRedirect()` (60a220f) extensions are configured in the `OnTopic.AspNetCore.Mvc.Host` project for integration testing.
2 parents 66c53c9 + 60a220f commit 86df229

4 files changed

Lines changed: 56 additions & 15 deletions

File tree

OnTopic.AspNetCore.Mvc.Host/SampleActivator.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ public object Create(ControllerContext context) {
102102
new TopicController(_topicRepository, _topicMappingService),
103103
nameof(SitemapController) =>
104104
new SitemapController(_topicRepository),
105+
nameof(RedirectController) =>
106+
new RedirectController(_topicRepository),
105107
_ => throw new Exception($"Unknown controller {type.Name}")
106108
};
107109

OnTopic.AspNetCore.Mvc.Host/Startup.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,9 @@ public static void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
112112
| Configure: MVC
113113
\-----------------------------------------------------------------------------------------------------------------------*/
114114
app.UseEndpoints(endpoints => {
115-
endpoints.MapControllerRoute(
116-
name: "default",
117-
pattern: "{controller}/{action=Index}/"
118-
);
119115
endpoints.MapTopicRoute("Web");
116+
endpoints.MapTopicSitemap();
117+
endpoints.MapTopicRedirect();
120118
endpoints.MapControllers();
121119
});
122120

OnTopic.AspNetCore.Mvc/Controllers/SitemapController.cs

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,9 @@ public SitemapController(ITopicRepository topicRepository) {
9191
/// Provides the Sitemap.org sitemap for the site.
9292
/// </summary>
9393
/// <param name="indent">Optionally enables indentation of XML elements in output for human readability.</param>
94-
/// <returns>The site's homepage view.</returns>
95-
public virtual ActionResult Index(bool indent = false) {
94+
/// <param name="includeMetadata">Optionally enables extended metadata associated with each topic.</param>
95+
/// <returns>A Sitemap.org sitemap.</returns>
96+
public virtual ActionResult Index(bool indent = false, bool includeMetadata = false) {
9697

9798
/*------------------------------------------------------------------------------------------------------------------------
9899
| Ensure topics are loaded
@@ -109,7 +110,7 @@ public virtual ActionResult Index(bool indent = false) {
109110
| Establish sitemap
110111
\-----------------------------------------------------------------------------------------------------------------------*/
111112
var declaration = new XDeclaration("1.0", "utf-8", "no");
112-
var sitemap = GenerateSitemap(rootTopic);
113+
var sitemap = GenerateSitemap(rootTopic, includeMetadata);
113114
var settings = indent? SaveOptions.None : SaveOptions.DisableFormatting;
114115

115116
/*------------------------------------------------------------------------------------------------------------------------
@@ -119,19 +120,36 @@ public virtual ActionResult Index(bool indent = false) {
119120

120121
}
121122

123+
/*==========================================================================================================================
124+
| GET: /SITEMAP/EXTENDED
125+
\-------------------------------------------------------------------------------------------------------------------------*/
126+
/// <summary>
127+
/// Provides the Sitemap.org sitemap for the site, including extended metadata attributes.
128+
/// </summary>
129+
/// <remarks>
130+
/// Introducing the metadata makes the sitemap considerably larger. However, it also means that some agents will index the
131+
/// additional information and make it available for querying. For instance, the (now defunct) Google Custom Search Engine
132+
/// (CSE) would previously allow queries to be filtered based on metadata attributes exposed via the sitemap.
133+
/// </remarks>
134+
/// <param name="indent">Optionally enables indentation of XML elements in output for human readability.</param>
135+
/// <returns>A Sitemap.org sitemap.</returns>
136+
public virtual ActionResult Extended(bool indent = false) => Index(indent, true);
137+
122138
/*==========================================================================================================================
123139
| METHOD: GENERATE SITEMAP
124140
\-------------------------------------------------------------------------------------------------------------------------*/
125141
/// <summary>
126142
/// Given a root topic, generates an XML-formatted sitemap.
127143
/// </summary>
128-
/// <returns>The site's homepage view.</returns>
144+
/// <param name="topic">The topic to add to the sitemap.</param>
145+
/// <param name="includeMetadata">Optionally enables extended metadata associated with each topic.</param>
146+
/// <returns>A Sitemap.org sitemap.</returns>
129147
[Obsolete("The GenerateSitemap() method should not be public. It will be marked private in OnTopic Library 5.0.")]
130-
public virtual XDocument GenerateSitemap(Topic rootTopic) =>
148+
public virtual XDocument GenerateSitemap(Topic rootTopic, bool includeMetadata = false) =>
131149
new XDocument(
132150
new XElement(_sitemapNamespace + "urlset",
133151
from topic in rootTopic?.Children
134-
select AddTopic(topic)
152+
select AddTopic(topic, includeMetadata)
135153
)
136154
);
137155

@@ -141,8 +159,10 @@ select AddTopic(topic)
141159
/// <summary>
142160
/// Given a <see cref="Topic"/>, adds it to a given <see cref="XmlNode"/>.
143161
/// </summary>
162+
/// <param name="topic">The topic to add to the sitemap.</param>
163+
/// <param name="includeMetadata">Optionally enables extended metadata associated with each topic.</param>
144164
[Obsolete("The AddTopic() method should not be public. It will be marked private in OnTopic Library 5.0.")]
145-
public IEnumerable<XElement> AddTopic(Topic topic) {
165+
public IEnumerable<XElement> AddTopic(Topic topic, bool includeMetadata = false) {
146166

147167
/*------------------------------------------------------------------------------------------------------------------------
148168
| Establish return collection
@@ -171,21 +191,23 @@ public IEnumerable<XElement> AddTopic(Topic topic) {
171191
new XElement(_sitemapNamespace + "changefreq", "monthly"),
172192
new XElement(_sitemapNamespace + "lastmod", lastModified.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)),
173193
new XElement(_sitemapNamespace + "priority", 1),
174-
new XElement(_pagemapNamespace + "PageMap",
194+
includeMetadata? new XElement(_pagemapNamespace + "PageMap",
175195
new XElement(_pagemapNamespace + "DataObject",
176196
new XAttribute("type", topic.ContentType?? "Page"),
177197
getAttributes()
178198
),
179199
getRelationships()
180-
)
200+
) : null
181201
);
182-
topics.Add(topicElement);
202+
if (!topic.ContentType!.Equals("Container", StringComparison.InvariantCultureIgnoreCase)) {
203+
topics.Add(topicElement);
204+
}
183205

184206
/*------------------------------------------------------------------------------------------------------------------------
185207
| Iterate over children
186208
\-----------------------------------------------------------------------------------------------------------------------*/
187209
foreach (var childTopic in topic.Children) {
188-
topics.AddRange(AddTopic(childTopic));
210+
topics.AddRange(AddTopic(childTopic, includeMetadata));
189211
}
190212

191213
return topics;

OnTopic.AspNetCore.Mvc/ServiceCollectionExtensions.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,25 @@ public static void MapImplicitAreaControllerRoute(this IEndpointRouteBuilder rou
209209
public static void MapImplicitAreaControllerRoute(this IEndpointRouteBuilder routes) =>
210210
routes.MapDynamicControllerRoute<TopicRouteValueTransformer>("{area:exists}/{action=Index}");
211211

212+
/*==========================================================================================================================
213+
| EXTENSION: MAP TOPIC SITEMAP (IENDPOINTROUTEBUILDER)
214+
\-------------------------------------------------------------------------------------------------------------------------*/
215+
/// <summary>
216+
/// Adds the <c>Sitemap/{action=Index}</c> endpoint route for the OnTopic sitemap.
217+
/// </summary>
218+
/// <remarks>
219+
/// For most implementations, this will be covered by the default route, such as that implemented by the standard <see
220+
/// cref="ControllerEndpointRouteBuilderExtensions.MapDefaultControllerRoute(IEndpointRouteBuilder)"/> method that ships
221+
/// with ASP.NET. This extension method is provided as a convenience method for implementations that aren't using the
222+
/// standard route, for whatever reason, and want a specific route setup for the sitemap.
223+
/// </remarks>
224+
public static ControllerActionEndpointConventionBuilder MapTopicSitemap(this IEndpointRouteBuilder routes) =>
225+
routes.MapControllerRoute(
226+
name: "TopicSitemap",
227+
pattern: "Sitemap/{action=Index}",
228+
defaults: new { controller = "Sitemap" }
229+
);
230+
212231
/*==========================================================================================================================
213232
| EXTENSION: MAP TOPIC REDIRECT (IENDPOINTROUTEBUILDER)
214233
\-------------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)