mirror of https://github.com/abpframework/abp.git
7 changed files with 86 additions and 64 deletions
@ -0,0 +1,48 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using JetBrains.Annotations; |
|||
|
|||
namespace Volo.Abp.Http |
|||
{ |
|||
public static class HttpMethodConventionHelper |
|||
{ |
|||
public const string DefaultHttpVerb = "POST"; |
|||
|
|||
public static Dictionary<string, string[]> ConventionalPrefixes { get; set; } = new Dictionary<string, string[]> |
|||
{ |
|||
{"GET", new[] {"GetList", "GetAll", "Get"}}, |
|||
{"PUT", new[] {"Put", "Update"}}, |
|||
{"DELETE", new[] {"Delete", "Remove"}}, |
|||
{"POST", new[] {"Create", "Add", "Insert", "Post"}}, |
|||
{"PATCH", new[] {"Patch"}} |
|||
}; |
|||
|
|||
public static string GetConventionalVerbForMethodName(string methodName) |
|||
{ |
|||
foreach (var conventionalPrefix in ConventionalPrefixes) |
|||
{ |
|||
if (conventionalPrefix.Value.Any(prefix => methodName.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))) |
|||
{ |
|||
return conventionalPrefix.Key; |
|||
} |
|||
} |
|||
|
|||
return DefaultHttpVerb; |
|||
} |
|||
|
|||
public static string RemoveHttpMethodPrefix([NotNull] string methodName, [NotNull] string httpMethod) |
|||
{ |
|||
Check.NotNull(methodName, nameof(methodName)); |
|||
Check.NotNull(httpMethod, nameof(httpMethod)); |
|||
|
|||
var prefixes = ConventionalPrefixes.GetOrDefault(httpMethod); |
|||
if (prefixes.IsNullOrEmpty()) |
|||
{ |
|||
return methodName; |
|||
} |
|||
|
|||
return methodName.RemovePreFix(prefixes); |
|||
} |
|||
} |
|||
} |
|||
@ -1,44 +0,0 @@ |
|||
using System; |
|||
|
|||
namespace Volo.Abp.Http |
|||
{ |
|||
public class HttpVerbHelper //TODO: Internal?
|
|||
{ |
|||
public const string DefaultHttpVerb = "POST"; |
|||
|
|||
public static string GetConventionalVerbForMethodName(string methodName) |
|||
{ |
|||
if (methodName.StartsWith("Get", StringComparison.OrdinalIgnoreCase)) |
|||
{ |
|||
return "GET"; |
|||
} |
|||
|
|||
if (methodName.StartsWith("Put", StringComparison.OrdinalIgnoreCase) || |
|||
methodName.StartsWith("Update", StringComparison.OrdinalIgnoreCase)) |
|||
{ |
|||
return "PUT"; |
|||
} |
|||
|
|||
if (methodName.StartsWith("Delete", StringComparison.OrdinalIgnoreCase) || |
|||
methodName.StartsWith("Remove", StringComparison.OrdinalIgnoreCase)) |
|||
{ |
|||
return "DELETE"; |
|||
} |
|||
|
|||
if (methodName.StartsWith("Patch", StringComparison.OrdinalIgnoreCase)) |
|||
{ |
|||
return "PATCH"; |
|||
} |
|||
|
|||
if (methodName.StartsWith("Post", StringComparison.OrdinalIgnoreCase) || |
|||
methodName.StartsWith("Create", StringComparison.OrdinalIgnoreCase) || |
|||
methodName.StartsWith("Insert", StringComparison.OrdinalIgnoreCase)) |
|||
{ |
|||
return "POST"; |
|||
} |
|||
|
|||
//Default
|
|||
return DefaultHttpVerb; |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue