From f0563bd00b9a027bcd1150c4ac42883b6fd9d62e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 4 Oct 2017 16:24:15 +0300 Subject: [PATCH] API improvements. --- .../AspNetCore/Mvc/AbpAppServiceConvention.cs | 4 +- .../AspNetCoreApiDescriptionModelProvider.cs | 50 +++++++++++++------ .../Mvc/UrlControllerNameNormalizerContext.cs | 13 +---- .../JQuery/JQueryProxyScriptGenerator.cs | 2 +- 4 files changed, 40 insertions(+), 29 deletions(-) diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAppServiceConvention.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAppServiceConvention.cs index 0952934df6..dd261936e2 100644 --- a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAppServiceConvention.cs +++ b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAppServiceConvention.cs @@ -311,9 +311,7 @@ namespace Volo.Abp.AspNetCore.Mvc return configuration.UrlControllerNameNormalizer( new UrlControllerNameNormalizerContext( rootPath, - controllerName, - action, - httpMethod + controllerName ) ); } diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs index f9802f2103..e41fa04806 100644 --- a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs +++ b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs @@ -1,8 +1,8 @@ using System; -using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; +using JetBrains.Annotations; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.AspNetCore.Mvc.ApiExplorer; @@ -10,6 +10,7 @@ using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Options; +using Volo.Abp.Application.Services; using Volo.Abp.AspNetCore.Mvc.Utils; using Volo.Abp.DependencyInjection; using Volo.Abp.Http.Modeling; @@ -38,6 +39,8 @@ namespace Volo.Abp.AspNetCore.Mvc public ApplicationApiDescriptionModel CreateApiModel() { + //TODO: Can cache the model? + var model = ApplicationApiDescriptionModel.Create(); foreach (var descriptionGroupItem in _descriptionProvider.ApiDescriptionGroups.Items) @@ -59,10 +62,11 @@ namespace Volo.Abp.AspNetCore.Mvc private void AddApiDescriptionToModel(ApiDescription apiDescription, ApplicationApiDescriptionModel model) { var controllerType = apiDescription.ActionDescriptor.AsControllerActionDescriptor().ControllerTypeInfo.AsType(); + var setting = FindSetting(controllerType); - var moduleModel = model.GetOrAddModule(GetRootPath(controllerType)); + var moduleModel = model.GetOrAddModule(GetRootPath(controllerType, setting)); - var controllerModel = moduleModel.GetOrAddController(controllerType.FullName, controllerType, _modelOptions.IgnoredInterfaces); + var controllerModel = moduleModel.GetOrAddController(CalculateControllerName(controllerType, setting), controllerType, _modelOptions.IgnoredInterfaces); var method = apiDescription.ActionDescriptor.GetMethodInfo(); @@ -83,6 +87,18 @@ namespace Volo.Abp.AspNetCore.Mvc AddParameterDescriptionsToModel(actionModel, method, apiDescription); } + private static string CalculateControllerName(Type controllerType, AbpControllerAssemblySetting setting) + { + var controllerName = controllerType.Name.RemovePostFix("Controller").RemovePostFix(ApplicationService.CommonPostfixes); + + if (setting?.UrlControllerNameNormalizer != null) + { + controllerName = setting.UrlControllerNameNormalizer(new UrlControllerNameNormalizerContext(setting.RootPath, controllerName)); + } + + return controllerName; + } + private static string GetUniqueActionName(MethodInfo method) { var methodNameBuilder = new StringBuilder(method.Name); @@ -152,19 +168,11 @@ namespace Volo.Abp.AspNetCore.Mvc return modelNameProvider.Name; } - private string GetRootPath(Type controllerType) + private static string GetRootPath([NotNull] Type controllerType, [CanBeNull] AbpControllerAssemblySetting setting) { - if (controllerType == null) + if (setting != null) { - return ModuleApiDescriptionModel.DefaultRootPath; - } - - foreach (var controllerSetting in _options.AppServiceControllers.ControllerAssemblySettings) - { - if(controllerSetting.ControllerTypes.Contains(controllerType)) - { - return controllerSetting.RootPath; - } + return setting.RootPath; } var areaAttr = controllerType.GetCustomAttributes().OfType().FirstOrDefault(); @@ -175,5 +183,19 @@ namespace Volo.Abp.AspNetCore.Mvc return ModuleApiDescriptionModel.DefaultRootPath; } + + [CanBeNull] + private AbpControllerAssemblySetting FindSetting(Type controllerType) + { + foreach (var controllerSetting in _options.AppServiceControllers.ControllerAssemblySettings) + { + if (controllerSetting.ControllerTypes.Contains(controllerType)) + { + return controllerSetting; + } + } + + return null; + } } } diff --git a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/UrlControllerNameNormalizerContext.cs b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/UrlControllerNameNormalizerContext.cs index 90bffe8df4..aad40fe4d7 100644 --- a/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/UrlControllerNameNormalizerContext.cs +++ b/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/UrlControllerNameNormalizerContext.cs @@ -1,24 +1,15 @@ -using Microsoft.AspNetCore.Mvc.ApplicationModels; - -namespace Volo.Abp.AspNetCore.Mvc +namespace Volo.Abp.AspNetCore.Mvc { - //TODO: Re-consider properties of this class. public class UrlControllerNameNormalizerContext { public string RootPath { get; } public string ControllerName { get; } - public ActionModel Action { get; } - - public string HttpMethod { get; } - - public UrlControllerNameNormalizerContext(string rootPath, string controllerName, ActionModel action, string httpMethod) + public UrlControllerNameNormalizerContext(string rootPath, string controllerName) { RootPath = rootPath; ControllerName = controllerName; - Action = action; - HttpMethod = httpMethod; } } } \ No newline at end of file diff --git a/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/JQuery/JQueryProxyScriptGenerator.cs b/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/JQuery/JQueryProxyScriptGenerator.cs index 87dbba1bad..299a9855e4 100644 --- a/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/JQuery/JQueryProxyScriptGenerator.cs +++ b/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/JQuery/JQueryProxyScriptGenerator.cs @@ -74,7 +74,7 @@ namespace Volo.Abp.Http.ProxyScripting.Generators.JQuery var parameterList = ProxyScriptingJsFuncHelper.GenerateJsFuncParameterList(action, "ajaxParams"); script.AppendLine($" // action '{action.NameOnClass.ToCamelCase()}'"); - script.AppendLine($" abp.services.{module.RootPath.Replace("/", ".").ToCamelCase()}.{controller.ControllerName.ToCamelCase()}{ProxyScriptingJsFuncHelper.WrapWithBracketsOrWithDotPrefix(action.NameOnClass.ToCamelCase())} = function({parameterList}) {{"); + script.AppendLine($" abp.services.{module.RootPath.Replace("/", ".").ToCamelCase()}.{controller.ControllerName.ToCamelCase()}{ProxyScriptingJsFuncHelper.WrapWithBracketsOrWithDotPrefix(action.NameOnClass.RemovePostFix("Async").ToCamelCase())} = function({parameterList}) {{"); script.AppendLine(" return abp.ajax($.extend(true, {"); AddAjaxCallParameters(script, controller, action);