This commit is contained in:
2021-11-30 15:35:50 +01:00
parent 694e8732fe
commit e65b0801fc
8 changed files with 87 additions and 33 deletions
+1 -7
View File
@@ -22,8 +22,7 @@ public class ZeroApiPlugin : ZeroPlugin
public override void ConfigureServices(IServiceCollection services, IConfiguration configuration)
{
services.AddOptions<ApiOptions>().Bind(configuration.GetSection("Zero:Api")).Configure<IWebHostEnvironment>(ConfigureOptions);
services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>, ZeroBackofficeMvcOptions>());
services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<MvcOptions>, ZeroApiMvcOptions>());
//Mvc.AddNewtonsoftJson(x =>
//{
// // TODO this shall only be configurated for backoffice controllers
@@ -34,11 +33,6 @@ public class ZeroApiPlugin : ZeroPlugin
services.AddZeroBackofficeModules(configuration);
services.AddControllers(opts =>
{
opts.Conventions.Add(new RouteTokenTransformerConvention(new ApiParameterTransformer()));
});
//services.AddTransient<ISectionsApi, SectionsApi>();
//services.AddTransient<ISettingsApi, SettingsApi>();
@@ -0,0 +1,43 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using System.Text;
namespace zero.Api.Controllers;
public class ZeroApiControllerModelConvention : IControllerModelConvention
{
readonly AttributeRouteModel RouteModel;
readonly Type BaseClass = typeof(ZeroApiController);
public ZeroApiControllerModelConvention(string zeroPath, string apiPath = "api", bool isAppAware = false)
{
StringBuilder path = new();
path.Append(zeroPath.EnsureEndsWith('/'));
path.Append(apiPath.TrimStart('/').EnsureEndsWith('/'));
if (isAppAware)
{
path.Append("{zero_app_slug}/");
}
path.Append("[controller]");
RouteModel = new AttributeRouteModel(new RouteAttribute(path.ToString()));
}
/// <summary>
/// Configure routing model for all backoffice controllers
/// </summary>
public void Apply(ControllerModel controller)
{
bool hasAttributeRouteModels = controller.Selectors.Any(selector => selector.AttributeRouteModel != null);
if (controller.ControllerType.IsSubclassOf(BaseClass))
{
controller.Selectors[0].AttributeRouteModel = RouteModel;
}
}
}
+24
View File
@@ -0,0 +1,24 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace zero.Api;
internal class ZeroApiMvcOptions : IConfigureOptions<MvcOptions>
{
IZeroOptions Options { get; set; }
public ZeroApiMvcOptions(IZeroOptions options)
{
Options = options;
}
public void Configure(MvcOptions options)
{
options.Conventions.Add(new ZeroApiControllerModelConvention(Options.ZeroPath, isAppAware: Options.For<ApplicationOptions>().EnableMultiple));
options.Conventions.Add(new RouteTokenTransformerConvention(new ApiParameterTransformer()));
}
}
-22
View File
@@ -1,22 +0,0 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.Extensions.Options;
using zero.Api.Filters;
namespace zero.Api;
internal class ZeroBackofficeMvcOptions : IConfigureOptions<MvcOptions>
{
IZeroOptions Options { get; set; }
public ZeroBackofficeMvcOptions(IZeroOptions options)
{
Options = options;
}
public void Configure(MvcOptions options)
{
options.Conventions.Add(new ZeroBackofficeControllerModelConvention(Options.ZeroPath));
//options.Conventions.Add(new RouteTokenTransformerConvention(new ApiParameterTransformer()));
}
}
@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using zero.Api.Controllers;
namespace zero.Backoffice.Controllers;
[ApiController]
[ZeroAuthorize]
[DisableBrowserCache]
//[ServiceFilter(typeof(ModelStateValidationFilterAttribute))]
//[ServiceFilter(typeof(BackofficeFilterAttribute))]
public abstract class ZeroBackofficeController : ControllerBase
{
IZeroMapper _mapper;
protected IZeroMapper Mapper => _mapper ?? (_mapper = HttpContext?.RequestServices?.GetService<IZeroMapper>());
}
@@ -1,13 +1,13 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApplicationModels;
namespace zero.Api.Controllers;
namespace zero.Backoffice;
public class ZeroBackofficeControllerModelConvention : IApplicationModelConvention
{
readonly AttributeRouteModel RouteModel;
readonly Type BaseClass = typeof(ZeroApiController);
readonly Type BaseClass = typeof(ZeroBackofficeController);
public ZeroBackofficeControllerModelConvention(string backofficePath)
@@ -1,6 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using zero.Api.Controllers;
namespace zero.Backoffice;
+1 -1
View File
@@ -14,8 +14,8 @@ public class ZeroApplicationBuilder : IZeroApplicationBuilder
App = app;
App.UseStaticFiles();
App.UseExceptionHandler("/zero/api/error");
App.UseMiddleware<ZeroContextMiddleware>();
App.UseRouting();
App.UseMiddleware<ZeroContextMiddleware>();
App.UseAuthentication();
App.UseAuthorization();