Browse Source

Revise action name normalization.

pull/116/head
Halil İbrahim Kalkan 9 years ago
parent
commit
e3d835578b
  1. 28
      src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAppServiceConvention.cs
  2. 3
      src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpControllerAssemblySetting.cs
  3. 6
      src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpControllerAssemblySettingBuilder.cs
  4. 7
      src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AppServiceControllerOptions.cs
  5. 20
      src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/DefaultUrlActionNameNormalizer.cs
  6. 7
      src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/IUrlActionNameNormalizer.cs
  7. 9
      src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/UrlActionNameNormalizerContext.cs
  8. 14
      test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/App/AbpAspNetCoreMvcTestModule.cs
  9. 17
      test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/App/PhoneBookUrlActionNameNormalizer.cs

28
src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpAppServiceConvention.cs

@ -214,7 +214,7 @@ namespace Volo.Abp.AspNetCore.Mvc
return HttpMethodHelper.GetConventionalVerbForMethodName(action.ActionName); return HttpMethodHelper.GetConventionalVerbForMethodName(action.ActionName);
} }
protected virtual void NormalizeSelectorRoutes(string rootPath, string controllerName, ActionModel action, AbpControllerAssemblySetting configuration) protected virtual void NormalizeSelectorRoutes(string rootPath, string controllerName, ActionModel action, [CanBeNull] AbpControllerAssemblySetting configuration)
{ {
foreach (var selector in action.Selectors) foreach (var selector in action.Selectors)
{ {
@ -238,7 +238,7 @@ namespace Volo.Abp.AspNetCore.Mvc
return _options.AppServiceControllers.ControllerAssemblySettings.GetSettingOrNull(controllerType); return _options.AppServiceControllers.ControllerAssemblySettings.GetSettingOrNull(controllerType);
} }
protected virtual AttributeRouteModel CreateAbpServiceAttributeRouteModel(string rootPath, string controllerName, ActionModel action, string httpMethod, AbpControllerAssemblySetting configuration) protected virtual AttributeRouteModel CreateAbpServiceAttributeRouteModel(string rootPath, string controllerName, ActionModel action, string httpMethod, [CanBeNull] AbpControllerAssemblySetting configuration)
{ {
return new AttributeRouteModel( return new AttributeRouteModel(
new RouteAttribute( new RouteAttribute(
@ -247,7 +247,7 @@ namespace Volo.Abp.AspNetCore.Mvc
); );
} }
protected virtual string CalculateRouteTemplate(string rootPath, string controllerName, ActionModel action, string httpMethod, AbpControllerAssemblySetting configuration) protected virtual string CalculateRouteTemplate(string rootPath, string controllerName, ActionModel action, string httpMethod, [CanBeNull] AbpControllerAssemblySetting configuration)
{ {
var controllerNameInUrl = NormalizeUrlControllerName(rootPath, controllerName, action, httpMethod, configuration); var controllerNameInUrl = NormalizeUrlControllerName(rootPath, controllerName, action, httpMethod, configuration);
@ -260,7 +260,7 @@ namespace Volo.Abp.AspNetCore.Mvc
} }
//Add action name if needed //Add action name if needed
var actionNameInUrl = NormalizeUrlActionName(rootPath, controllerName, action, httpMethod); var actionNameInUrl = NormalizeUrlActionName(rootPath, controllerName, action, httpMethod, configuration);
if (!actionNameInUrl.IsNullOrEmpty()) if (!actionNameInUrl.IsNullOrEmpty())
{ {
url += $"/{actionNameInUrl.ToCamelCase()}"; url += $"/{actionNameInUrl.ToCamelCase()}";
@ -276,16 +276,26 @@ namespace Volo.Abp.AspNetCore.Mvc
return url; return url;
} }
protected virtual string NormalizeUrlActionName(string rootPath, string controllerName, ActionModel action, string httpMethod) protected virtual string NormalizeUrlActionName(string rootPath, string controllerName, ActionModel action, string httpMethod, [CanBeNull] AbpControllerAssemblySetting configuration)
{ {
var context = new UrlActionNameNormalizerContext(rootPath, controllerName, action, httpMethod, action.ActionName); var actionNameInUrl = HttpMethodHelper
.RemoveHttpMethodPrefix(action.ActionName, httpMethod)
.RemovePostFix("Async");
foreach (var normalizer in _options.AppServiceControllers.UrlActionNameNormalizers) if (configuration?.UrlActionNameNormalizer == null)
{ {
normalizer.Normalize(context); return actionNameInUrl;
} }
return context.ActionNameInUrl; return configuration.UrlActionNameNormalizer(
new UrlActionNameNormalizerContext(
rootPath,
controllerName,
action,
actionNameInUrl,
httpMethod
)
);
} }
protected virtual string NormalizeUrlControllerName(string rootPath, string controllerName, ActionModel action, string httpMethod, [CanBeNull] AbpControllerAssemblySetting configuration) protected virtual string NormalizeUrlControllerName(string rootPath, string controllerName, ActionModel action, string httpMethod, [CanBeNull] AbpControllerAssemblySetting configuration)

3
src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpControllerAssemblySetting.cs

@ -22,6 +22,9 @@ namespace Volo.Abp.AspNetCore.Mvc
[CanBeNull] [CanBeNull]
public Func<UrlControllerNameNormalizerContext, string> UrlControllerNameNormalizer { get; set; } public Func<UrlControllerNameNormalizerContext, string> UrlControllerNameNormalizer { get; set; }
[CanBeNull]
public Func<UrlActionNameNormalizerContext, string> UrlActionNameNormalizer { get; set; }
public AbpControllerAssemblySetting([NotNull] Assembly assembly, [NotNull] string rootPath) public AbpControllerAssemblySetting([NotNull] Assembly assembly, [NotNull] string rootPath)
{ {
Check.NotNull(assembly, rootPath); Check.NotNull(assembly, rootPath);

6
src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AbpControllerAssemblySettingBuilder.cs

@ -29,5 +29,11 @@ namespace Volo.Abp.AspNetCore.Mvc
_setting.UrlControllerNameNormalizer = normalizer; _setting.UrlControllerNameNormalizer = normalizer;
return this; return this;
} }
public AbpControllerAssemblySettingBuilder NormalizeActionNameInUrl(Func<UrlActionNameNormalizerContext, string> normalizer)
{
_setting.UrlActionNameNormalizer = normalizer;
return this;
}
} }
} }

7
src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AppServiceControllerOptions.cs

@ -12,8 +12,6 @@ namespace Volo.Abp.AspNetCore.Mvc
public List<Type> FormBodyBindingIgnoredTypes { get; } public List<Type> FormBodyBindingIgnoredTypes { get; }
public List<IUrlActionNameNormalizer> UrlActionNameNormalizers { get; }
public AppServiceControllerOptions() public AppServiceControllerOptions()
{ {
ControllerAssemblySettings = new ControllerAssemblySettingList(); ControllerAssemblySettings = new ControllerAssemblySettingList();
@ -22,11 +20,6 @@ namespace Volo.Abp.AspNetCore.Mvc
{ {
typeof(IFormFile) typeof(IFormFile)
}; };
UrlActionNameNormalizers = new List<IUrlActionNameNormalizer>
{
new DefaultUrlActionNameNormalizer()
};
} }
public AbpControllerAssemblySettingBuilder CreateFor(Assembly assembly, string rootPath = ModuleApiDescriptionModel.DefaultRootPath) public AbpControllerAssemblySettingBuilder CreateFor(Assembly assembly, string rootPath = ModuleApiDescriptionModel.DefaultRootPath)

20
src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/DefaultUrlActionNameNormalizer.cs

@ -1,20 +0,0 @@
using System;
using Volo.Abp.Http;
namespace Volo.Abp.AspNetCore.Mvc
{
public class DefaultUrlActionNameNormalizer : IUrlActionNameNormalizer
{
public void Normalize(UrlActionNameNormalizerContext context)
{
if (context.ActionNameInUrl.IsNullOrEmpty())
{
return;
}
context.ActionNameInUrl = HttpMethodHelper
.RemoveHttpMethodPrefix(context.ActionNameInUrl, context.HttpMethod)
.RemovePostFix("Async");
}
}
}

7
src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/IUrlActionNameNormalizer.cs

@ -1,7 +0,0 @@
namespace Volo.Abp.AspNetCore.Mvc
{
public interface IUrlActionNameNormalizer
{
void Normalize(UrlActionNameNormalizerContext context);
}
}

9
src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/UrlActionNameNormalizerContext.cs

@ -10,18 +10,17 @@ namespace Volo.Abp.AspNetCore.Mvc
public ActionModel Action { get; } public ActionModel Action { get; }
public string HttpMethod { get; } public string ActionNameInUrl { get; }
public string ActionNameInUrl { get; set; } public string HttpMethod { get; }
public UrlActionNameNormalizerContext(string rootPath, string controllerName, ActionModel action, string httpMethod, string actionNameInUrl) public UrlActionNameNormalizerContext(string rootPath, string controllerName, ActionModel action, string actionNameInUrl, string httpMethod)
{ {
RootPath = rootPath; RootPath = rootPath;
ControllerName = controllerName; ControllerName = controllerName;
Action = action; Action = action;
HttpMethod = httpMethod;
ActionNameInUrl = actionNameInUrl; ActionNameInUrl = actionNameInUrl;
HttpMethod = httpMethod;
} }
} }
} }

14
test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/App/AbpAspNetCoreMvcTestModule.cs

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Builder; using System;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Volo.Abp.AspNetCore.Modularity; using Volo.Abp.AspNetCore.Modularity;
using Volo.Abp.AspNetCore.Mvc; using Volo.Abp.AspNetCore.Mvc;
@ -24,8 +25,15 @@ namespace Volo.Abp.AspNetCore.App
services.Configure<AbpAspNetCoreMvcOptions>(options => services.Configure<AbpAspNetCoreMvcOptions>(options =>
{ {
options.AppServiceControllers.CreateFor(typeof(TestAppModule).Assembly); options
options.AppServiceControllers.UrlActionNameNormalizers.Add(new PhoneBookUrlActionNameNormalizer()); .AppServiceControllers
.CreateFor(typeof(TestAppModule).Assembly)
.NormalizeActionNameInUrl(
context =>
string.Equals(context.ActionNameInUrl, "phone", StringComparison.OrdinalIgnoreCase)
? "phones"
: context.ActionNameInUrl
);
}); });
services.AddAssemblyOf<AbpAspNetCoreMvcTestModule>(); services.AddAssemblyOf<AbpAspNetCoreMvcTestModule>();

17
test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/App/PhoneBookUrlActionNameNormalizer.cs

@ -1,17 +0,0 @@
using System;
using Volo.Abp.AspNetCore.Mvc;
namespace Volo.Abp.AspNetCore.App
{
public class PhoneBookUrlActionNameNormalizer : IUrlActionNameNormalizer
{
public void Normalize(UrlActionNameNormalizerContext context)
{
if (string.Equals(context.ActionNameInUrl, "phone", StringComparison.OrdinalIgnoreCase))
{
context.ActionNameInUrl = "phones";
return;
}
}
}
}
Loading…
Cancel
Save