Skip to content

Custom Asp.Net Core conventions that can be configured in the IMvcBuilder

License

Notifications You must be signed in to change notification settings

ogulcanturan/Ogu.AspNetCore.Conventions

Repository files navigation

Header Ogu.AspNetCore.Conventions

.NET NuGet Nuget

Introduction

Ogu.AspNetCore.Conventions is a library which comprising four essential conventions that can be configured in the IMvcBuilder.

Conventions

  • 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.

Installation

You can install the library via NuGet Package Manager:

dotnet add package Ogu.AspNetCore.Conventions

Usage

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>();
    });
    ...
}

Sample Application

A sample application demonstrating the usage of Ogu.AspNetCore.Conventions can be found here.

About

Custom Asp.Net Core conventions that can be configured in the IMvcBuilder

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages