Skip to content

Commit f41b76a

Browse files
committed
Introduced MapImplicitAreaControllerRoute() overload for mapping all areas
The existing `MapImplictAreaControllerRoute()` extension method recently introduced (8a65c95) 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. It does this by implementing the new `TopicRouteValueTransformer` (865f26a) to automatically set the `controller` routing variable to the `area` name, which is the convention we most frequently follow. Be aware that, as with the recently introduced `MapTopicAreaRoute()` (762d229), 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 762d229 commit f41b76a

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

OnTopic.AspNetCore.Mvc/ServiceCollectionExtensions.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,28 @@ public static void MapImplicitAreaControllerRoute(this IEndpointRouteBuilder rou
187187
defaults: new { controller = areaName }
188188
);
189189

190+
/// <summary>
191+
/// Adds the <c>{area:exists}/{action=Index}</c> endpoint route for all areas where the controller has the same name as
192+
/// the area.
193+
/// </summary>
194+
/// <remarks>
195+
/// <para>
196+
/// This extension method implicitly assigns the controller name based on the area name. This is advantageous when there
197+
/// are multiple areas which have a single controller which is named after the area—e.g., <c>[Area("Forms")]</c> and
198+
/// <c>FormsController: Controller</c>—as this allows those to be collectively registered under a single route, without
199+
/// needing the redundant <c>Controller</c> value to be defined in the route (e.g., <c>/Forms/Forms/{action}</c>.
200+
/// </para>
201+
/// <para>
202+
/// Be aware that this method uses the <see cref="ControllerEndpointRouteBuilderExtensions.MapDynamicControllerRoute{
203+
/// TTransformer}(IEndpointRouteBuilder, String)"/> method. In .NET 3.x, this is incompatible with both the <see cref=
204+
/// "AnchorTagHelper"/> and <see cref="LinkGenerator"/> classes. This means that e.g. <c>@Url.Action()</c> references
205+
/// in views won't be properly formed. If these are required, prefer registering each route individually using <see
206+
/// cref="MapImplicitAreaControllerRoute(IEndpointRouteBuilder, String)"/>.
207+
/// </para>
208+
/// </remarks>
209+
public static void MapImplicitAreaControllerRoute(this IEndpointRouteBuilder routes) =>
210+
routes.MapDynamicControllerRoute<TopicRouteValueTransformer>("{area:exists}/{action=Index}");
211+
190212
/*==========================================================================================================================
191213
| EXTENSION: MAP TOPIC REDIRECT (IENDPOINTROUTEBUILDER)
192214
\-------------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)