Skip to content

Commit 8a65c95

Browse files
committed
Introduced MapImplicitAreaControllerRoute()
The `MapImplicitAreaControllerRoute()` extension method is similar to the `MapDefaultAreaControllerRoute()` recently introduced (35cdd74), except that it supports a short-hand convention for scenarios where the controller name is the same as the area name. This allows e.g., a `[Area("Forms")] public FormsController: Controller` to be routed via `/Forms`, instead of the redundant `/Forms/Forms`. This convention is most commonly used for scenarios where there is a single controller for an area.
1 parent 35cdd74 commit 8a65c95

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

OnTopic.AspNetCore.Mvc/ServiceCollectionExtensions.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,32 @@ public static void MapDefaultAreaControllerRoute(this IEndpointRouteBuilder rout
143143
pattern: "{area:exists}/{controller}/{action=Index}/{id?}"
144144
);
145145

146+
/*==========================================================================================================================
147+
| EXTENSION: MAP IMPLICIT AREA CONTROLLER ROUTES (IENDPOINTROUTEBUILDER)
148+
\-------------------------------------------------------------------------------------------------------------------------*/
149+
/// <summary>
150+
/// Adds the <c>{areaName}/{action=Index}</c> endpoint route for a specific area where the controller has the same name as
151+
/// the area.
152+
/// </summary>
153+
/// <remarks>
154+
/// <para>
155+
/// This extension method implicitly assigns the controller name based on the area name. This is advantageous when an
156+
/// area has a single controller which is named after the area—e.g., <c>[Area("Forms")]</c> and <c>FormsController</c>—
157+
/// as this allows the redundant <c>Controller</c> to be ommited from the route (e.g., <c>/Forms/Forms/{action}</c>.
158+
/// </para>
159+
/// <para>
160+
/// If there are multiple routes that fit this description, you can instead opt to use the <see cref=
161+
/// "MapImplicitAreaControllerRoute(IEndpointRouteBuilder)"/> overload, which will register all areas.
162+
/// </para>
163+
/// </remarks>
164+
public static void MapImplicitAreaControllerRoute(this IEndpointRouteBuilder routes, string areaName) =>
165+
routes.MapAreaControllerRoute(
166+
name: $"{areaName}TopicArea",
167+
areaName: areaName,
168+
pattern: $"{areaName}/{{action}}",
169+
defaults: new { controller = areaName }
170+
);
171+
146172
/*==========================================================================================================================
147173
| EXTENSION: MAP TOPIC REDIRECT (IENDPOINTROUTEBUILDER)
148174
\-------------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)