My utility library is offering multiple actions that should be usable in different ASP.Net Core applications. So far, an extension method for IApplicationBuilder is used to configure the routes:
public static void UseUtility(this IApplicationBuilder app)
{
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
"default",
"api/{controller=Home}/{action=Index}/{id?}");
endpoints.MapDefaultControllerRoute().RequireAuthorization();
}
);
}
In order to register these routes with a configurable path (e.g. utility/), the hosting application calls Map on its IApplicationBuilder:
app.Map("utility", hostingApp => { hostingApp.UseUtility(); });
The problem is, according to Routing in ASP.NET Core a library is not supposed to call UseEndpoints, but offer an extension method for IEndpointRouteBuilder. So I tried the following:
public static void MapUtility(this IEndpointRouteBuilder routeBuilder)
{
routeBuilder.MapControllerRoute(
"default",
"api/{controller=Home}/{action=Index}/{id?}");
routeBuilder.MapDefaultControllerRoute().RequireAuthorization();
}
The hosting application calls it in Configure()
app.UseEndpoints(endpoints =>
{
endpoints.MapUtility();
}
);
Two questions arise here:
- How can I add the
utility/to the beginning of the routing path? - How to register only the enpoints/controllers of my utility library with the given route and not the controlles of the hosting application?

