Ogu.AspNetCore.Conventions is a library which comprising four essential conventions that can be configured in the IMvcBuilder.
- ControllerAuthorizeConvention: Simplify authorization configuration by defining authorization rules within your IOC container. This convention allows you to enforce access control policies on controller actions without the need for explicit AuthorizeAttribute annotations.
- ControllerDisableConvention: Instead of removing entire controller assemblies, this convention lets you disable individual controllers.
- ControllerRoutePrefixConvention: This convention eliminates the need to clutter your controller classes with RouteAttribute annotations, allowing for cleaner and more flexible routing configurations.
- ControllerHideFromExploringConvention: prevent controllers from appearing in API documentation.
You can install the library via NuGet Package Manager:
dotnet add package Ogu.AspNetCore.Conventions
ControllerAuthorizeConvention:
public virtual void ConfigureServices(IServiceCollection services)
{
...
services.AddControllers().AddMvcOptions(opts =>
{
// To configure with params => [Authorize(AuthenticationSchemes = "", Policy = "", Roles = "")]
opts.Conventions.AddControllerAuthorizeConvention<LauncherController>(conventionOpts =>
{
conventionOpts.AuthenticationSchemes = "";
conventionOpts.Policy = "";
conventionOpts.Roles = "";
});
// to use default => [Authorize]
opts.Conventions.AddControllerAuthorizeConvention<LauncherController>();
});
...
}
ControllerDisableConvention:
public virtual void ConfigureServices(IServiceCollection services)
{
...
services.AddControllers().AddMvcOptions(opts =>
{
opts.Conventions.AddControllerDisableConvention<LauncherController>();
});
...
}
ControllerRoutePrefixConvention:
public virtual void ConfigureServices(IServiceCollection services)
{
...
services.AddControllers().AddMvcOptions(opts =>
{
opts.Conventions.AddControllerRoutePrefixConvention<LauncherController>("api/launcher");
});
...
}
ControllerHideFromExploringConvention:
public virtual void ConfigureServices(IServiceCollection services)
{
...
services.AddControllers().AddMvcOptions(opts =>
{
opts.Conventions.AddControllerHideFromExploringConvention<LauncherController>();
});
...
}
A sample application demonstrating the usage of Ogu.AspNetCore.Conventions can be found here.