Minimal api alternative to ManagementApiControllerBase

Hi folks

I have a question about custom apis for the Backoffice / Management API

In Umbraco 17, do they need to be conventional controllers that implement ManagementApiControllerBase as per

Or is it possible using the minimal api pattern instead?

I have existing minimal apis that I would like lock down under the Backoffice / Management API umbrella but it proving difficult to find a working minimal api example for Umbraco 17.

One of my goals here is so that they appear under the Umbraco Management Api section in Swagger so can use that harness (and auth) to test against.

Can anybody help / has advice?

Many thanks

I think the ManagementApiBase does a lot of the lifting automatically. It makes sure it’s added to Swagger, it handles authentication so that you can only call the endpoint if you are actually a logged in user. I guess if the correct stuff is exposed publicly, you could add and implement that yourself (like the BackOfficeUser attribute). However, if the ManagementApiBase gets developed further, you miss out on those improvements.

Hmm I asked Claude. The ManagementApiBase by itself does not do that much and you can actually pretty easy create a minimal api controller yourself. However, adding to swagger seems to be the harder part and you need to fight the framework a little for it.

The ManagementApiControllerBase doesn’t really do much on its own. It’s an abstract controller whose main job is to stack a set of attributes that together make an endpoint “part of the Management API”. The helper methods on it are just conveniences.

The attributes it applies are:

  • [ApiController] for standard API behaviour and model validation
  • [Authorize(Policy = BackOfficeAccess)] which requires an authenticated backoffice user (this is the lock-down part)
  • [Authorize(Policy = UmbracoFeatureEnabled)] plus the IUmbracoFeature marker for feature gating
  • [MapToApi("management")] which groups the endpoint into the management OpenAPI document (this is what puts it in Swagger)
  • [JsonOptionsName(BackOffice)] for the backoffice JSON options
  • a few filters (AppendEventMessages, DisableBrowserCache, MaintenanceModeActionFilter)

Note it does not set the route. The /umbraco/management/api/v{version}/ prefix comes from a separate route convention, not from the base class. So being a Management API endpoint is really four separate concerns bundled together: routing, auth, OpenAPI grouping, and JSON options.

On whether the minimal API approach works:

Auth is fine. You can apply the same policy to a minimal endpoint:

app.MapGroup("/umbraco/management/api/v1/mything")
   .RequireAuthorization(AuthorizationPolicies.BackOfficeAccess);

Swagger is the catch. Umbraco decides what goes into a document in ConfigureUmbracoOpenApiOptionsBase.ShouldInclude. The [MapToApi] check only applies to ControllerActionDescriptor, so it never matches minimal APIs. The only other way in is the fallback that checks ApiVersionMetadata.Name == ApiName. So to get a minimal endpoint into the core management document you’d have to attach Asp.Versioning metadata whose version set is named management. That’s possible but hacky, and you’d be injecting into the document that generates the backoffice TypeScript client (which ships pre-generated as an embedded OpenApi.json).

The cleaner route, and what the official extension template does, is to not piggyback on the core document at all. Create your own named API with its own OpenAPI document:

[ApiController]
[BackOfficeRoute("umbracoextension/api/v{version:apiVersion}")]
[Authorize(Policy = AuthorizationPolicies.SectionAccessContent)]
[MapToApi(Constants.ApiName)]
public class MyApiControllerBase : ControllerBase { }

That gives you backoffice auth plus your own Swagger doc without touching core.

So short version: you can secure minimal APIs with the same policy easily, but forcing them into the core management Swagger doc is fighting the framework. If you just want them locked down and documented, give them their own API name and document rather than reproducing ManagementApiControllerBase against the management one.

ditto on the package extension template approach..

Umbraco-CMS/templates/UmbracoExtension/Controllers at main · umbraco/Umbraco-CMS

thanks for the help guys :slight_smile:

Yes i had this run this past agents too but i was only getting Umbraco 18 compatible suggestions i think (so maybe this minimal api approach is possible/supported but only in 18?)

I’ve gone with the ManagementApiControllerBase implementation as path of least resistance. I wondered if there were fluent apis i could chain in Umbraco 17 that would match (have parity) with what all those decorators do on ManagementApiControllerBase but umbraco 17 can’t quite be there as yet.

Instead of “guys”, may we suggest, for example: “folks", “Umbracians”, “all”, or “everyone”? We use gender inclusive language in this forum :grinning_face: (read more)

ok… my bad