Skip to content

Commit 762d229

Browse files
committed
Introduced MapTopicAreaRoute() overload for mapping all areas
The existing `MapTopicAreaRoute()` extension method recently introduced (6c6ad74) requires, at minimum, an `areaName` parameter, and thus is limited to setting up routes for one individual area at a time. This extension doesn't (currently) accept any parameters, and will instead map _all_ areas to support the `{**path}` catch-all expected by the OnTopic CMS. It does this by implementing the new `TopicRouteValueTransformer` (865f26a) to automatically set the `controller` and the `rootTopic` routing variables to the `area` name, which is the convention we most frequently follow. As part of this, I also registered `TopicRouteValueTransformer` with ASP.NET Core dependent injection, as part of the `AddTopicSupport()` extension. Be aware that this implementation trips of a feature limitation of ASP.NET Core 3.0 which prevents e.g. `@Url.Action()` references from correctly returning values (see dotnet/aspnetcore#16965). Hopefully that will be resolved in ASP.NET 4.0.
1 parent 8a65c95 commit 762d229

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

OnTopic.AspNetCore.Mvc/ServiceCollectionExtensions.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
| Client Ignia, LLC
44
| Project Topics Library
55
\=============================================================================================================================*/
6-
using OnTopic.AspNetCore.Mvc.Controllers;
7-
using OnTopic.Internal.Diagnostics;
6+
using System;
87
using Microsoft.AspNetCore.Builder;
98
using Microsoft.AspNetCore.Mvc.Infrastructure;
9+
using Microsoft.AspNetCore.Mvc.TagHelpers;
1010
using Microsoft.AspNetCore.Routing;
1111
using Microsoft.Extensions.DependencyInjection;
1212
using Microsoft.Extensions.DependencyInjection.Extensions;
13+
using OnTopic.AspNetCore.Mvc.Controllers;
14+
using OnTopic.Internal.Diagnostics;
1315

1416
namespace OnTopic.AspNetCore.Mvc {
1517

@@ -38,6 +40,7 @@ public static IMvcBuilder AddTopicSupport(this IMvcBuilder services) {
3840
| Register services
3941
\-----------------------------------------------------------------------------------------------------------------------*/
4042
services.Services.TryAddSingleton<IActionResultExecutor<TopicViewResult>, TopicViewResultExecutor>();
43+
services.Services.TryAddSingleton<TopicRouteValueTransformer>();
4144

4245
/*------------------------------------------------------------------------------------------------------------------------
4346
| Configure services
@@ -127,6 +130,21 @@ public static ControllerActionEndpointConventionBuilder MapTopicAreaRoute(
127130
defaults: new { controller = controller?? areaName, action, rootTopic = areaName }
128131
);
129132

133+
/// <summary>
134+
/// Adds the <c>{area:exists}/{**path}</c> endpoint route for all areas, which enables the areas to be mapped to specific
135+
/// topics via the <see cref="TopicRepositoryExtensions.Load(Repositories.ITopicRepository, RouteData)"/> extension method
136+
/// used by <see cref="TopicController"/>.
137+
/// </summary>
138+
/// <remarks>
139+
/// Be aware that this method uses the <see cref="ControllerEndpointRouteBuilderExtensions.MapDynamicControllerRoute{
140+
/// TTransformer}(IEndpointRouteBuilder, String)"/> method. In .NET 3.x, this is incompatible with both the <see cref=
141+
/// "AnchorTagHelper"/> and <see cref="LinkGenerator"/> classes. This means that e.g. <c>@Url.Action()</c> references
142+
/// in views won't be properly formed. If these are required, prefer registering each route individually using <see cref=
143+
/// "MapTopicAreaRoute(IEndpointRouteBuilder, String, String?, String)"/>.
144+
/// </remarks>
145+
public static void MapTopicAreaRoute(this IEndpointRouteBuilder routes) =>
146+
routes.MapDynamicControllerRoute<TopicRouteValueTransformer>("{area:exists}/{**path}");
147+
130148
/*==========================================================================================================================
131149
| EXTENSION: MAP DEFAULT AREA CONTROLLER ROUTES (IENDPOINTROUTEBUILDER)
132150
\-------------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)