namespace Kit.Helpers.Routes { using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Routing; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; public class ControllerBaseRouteFluent : IEndPointFluent> where TController : ControllerBase { private readonly IRouteBuilder _routeTable; private readonly List _routes; public ControllerBaseRouteFluent(IRouteBuilder routeTable) { _routeTable = routeTable; _routes = new List(); } #region Generic Result Actions private void AddRouteLambda(string url, LambdaExpression actionSelector) { // получаем имя контроллера string controllerName = typeof(TController).Name; if (controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase) && controllerName.Length > "Controller".Length) { controllerName = controllerName.Remove(controllerName.Length - "Controller".Length, "Controller".Length); } // получаем имя действия var unaryExpression = (UnaryExpression)actionSelector.Body; var methodCallExpression = (MethodCallExpression)unaryExpression.Operand; var constantExpression = (ConstantExpression)methodCallExpression.Object; var methodInfo = (MethodInfo)constantExpression.Value; string actionName = methodInfo.Name; _routes.Add( _routeTable.AddRoute($"{controllerName}_{actionName}_{Guid.NewGuid()}",url, controllerName, actionName, methodInfo)); } public delegate TResult GenericResult0Delegate(); public delegate TResult GenericResult1Delegate(TInput1 input1); public delegate TResult GenericResult2Delegate(TInput1 input1, TInput2 input2); public delegate TResult GenericResult3Delegate(TInput1 input1, TInput2 input2, TInput3 input3); public delegate TResult GenericResult4Delegate(TInput1 input1, TInput2 input2, TInput3 input3, TInput4 input4); public delegate TResult GenericResult5Delegate(TInput1 input1, TInput2 input2, TInput3 input3, TInput4 input4, TInput5 input5); public ControllerBaseRouteFluent GenericAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent GenericAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent GenericAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent GenericAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent GenericAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent GenericAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } #endregion #region JsonResult Actions public ControllerBaseRouteFluent JsonAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent JsonAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent JsonAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent JsonAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent JsonAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent JsonAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } #endregion #region EmptyResult Actions public ControllerBaseRouteFluent EmptyAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent EmptyAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent EmptyAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent EmptyAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent EmptyAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent EmptyAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } #endregion #region ContentResult Actions public ControllerBaseRouteFluent ContentAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent ContentAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent ContentAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent ContentAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent ContentAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent ContentAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } #endregion #region PartialViewResult Actions public ControllerBaseRouteFluent PartialViewAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent PartialViewAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent PartialViewAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent PartialViewAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent PartialViewAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent PartialViewAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } #endregion #region ActionResult Actions public ControllerBaseRouteFluent ActionAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent ActionAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent ActionAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent ActionAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent ActionAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerBaseRouteFluent ActionAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } #endregion #region IRouteFluent> public ControllerBaseRouteFluent NeedHttpGet(bool required = true) { if (_routes.Any()) _routes.Last().NeedHttpGet(required); return this; } public ControllerBaseRouteFluent NeedHttpPost(bool required = true) { if (_routes.Any()) _routes.Last().NeedHttpPost(required); return this; } public ControllerBaseRouteFluent WithDefaults(object defaults) { if (_routes.Any()) _routes.Last().WithDefaults(defaults); return this; } public ControllerBaseRouteFluent WithConstraints(object constraints) { if (_routes.Any()) _routes.Last().WithConstraints(constraints); return this; } public ControllerBaseRouteFluent WithNamespaces(string[] namespaces) { if (_routes.Any()) _routes.Last().WithNamespaces(namespaces); return this; } public ControllerBaseRouteFluent NeedSecurityKey(string securityKeyName) { if (_routes.Any()) _routes.Last().NeedSecurityKey(securityKeyName); return this; } public ControllerBaseRouteFluent WithDataTokens(object dataTokens) { if (_routes.Any()) _routes.Last().WithDataTokens(dataTokens); return this; } public ControllerBaseRouteFluent WithDelegate(Action routeFluentAction) { if (_routes.Any()) routeFluentAction(_routes.Last()); return this; } #endregion #region IRouteFluent> for ALL public ControllerBaseRouteFluent NeedHttpGet_All(bool required = true) { _routes.ForEach(x => x.NeedHttpGet(required)); return this; } public ControllerBaseRouteFluent NeedHttpPost_All(bool required = true) { _routes.ForEach(x => x.NeedHttpPost(required)); return this; } public ControllerBaseRouteFluent WithDefaults_All(object defaults) { _routes.ForEach(x => x.WithDefaults(defaults)); return this; } public ControllerBaseRouteFluent WithConstraints_All(object constraints) { _routes.ForEach(x => x.WithConstraints(constraints)); return this; } public ControllerBaseRouteFluent WithNamespaces_All(string[] namespaces) { _routes.ForEach(x => x.WithNamespaces(namespaces)); return this; } public ControllerBaseRouteFluent NeedSecurityKey_All(string securityKeyName) { _routes.ForEach(x => x.NeedSecurityKey(securityKeyName)); return this; } public ControllerBaseRouteFluent WithDataTokens_All(object dataTokens) { _routes.ForEach(x => x.WithDataTokens(dataTokens)); return this; } public ControllerBaseRouteFluent WithDelegate_All(Action routeFluentAction) { _routes.ForEach(x => routeFluentAction(x)); return this; } #endregion } }