namespace Kit.Helpers.Routes { using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Routing; //using Microsoft.AspNetCore.Routing; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Reflection; using System.Xml.Linq; public class ControllerRouteFluent : IEndPointFluent> where TController : ControllerBase { private readonly IRouteBuilder _routeTable; private readonly List _routes; public ControllerRouteFluent(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)); } 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 ControllerRouteFluent GenericAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent GenericAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent GenericAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent GenericAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent GenericAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent GenericAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } #endregion #region JsonResult Actions public ControllerRouteFluent JsonAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent JsonAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent JsonAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent JsonAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent JsonAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent JsonAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } #endregion #region EmptyResult Actions public ControllerRouteFluent EmptyAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent EmptyAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent EmptyAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent EmptyAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent EmptyAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent EmptyAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } #endregion #region ContentResult Actions public ControllerRouteFluent ContentAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent ContentAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent ContentAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent ContentAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent ContentAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent ContentAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } #endregion #region PartialViewResult Actions public ControllerRouteFluent PartialViewAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent PartialViewAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent PartialViewAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent PartialViewAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent PartialViewAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent PartialViewAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } #endregion #region ActionResult Actions public ControllerRouteFluent ActionAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent ActionAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent ActionAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent ActionAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent ActionAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } public ControllerRouteFluent ActionAction(string url, Expression>> actionSelector) { this.AddRouteLambda(url, actionSelector); return this; } #endregion #region IRouteFluent> public ControllerRouteFluent NeedHttpGet(bool required = true) { if (_routes.Any()) _routes.Last().NeedHttpGet(required); return this; } public ControllerRouteFluent NeedHttpPost(bool required = true) { if (_routes.Any()) _routes.Last().NeedHttpPost(required); return this; } public ControllerRouteFluent WithDefaults(object defaults) { if (_routes.Any()) _routes.Last().WithDefaults(defaults); return this; } public ControllerRouteFluent WithConstraints(object constraints) { if (_routes.Any()) _routes.Last().WithConstraints(constraints); return this; } public ControllerRouteFluent WithNamespaces(string[] namespaces) { if (_routes.Any()) _routes.Last().WithNamespaces(namespaces); return this; } public ControllerRouteFluent NeedSecurityKey(string securityKeyName) { if (_routes.Any()) _routes.Last().NeedSecurityKey(securityKeyName); return this; } public ControllerRouteFluent WithDataTokens(object dataTokens) { if (_routes.Any()) _routes.Last().WithDataTokens(dataTokens); return this; } public ControllerRouteFluent WithDelegate(Action routeFluentAction) { if (_routes.Any()) routeFluentAction(_routes.Last()); return this; } #endregion #region IRouteFluent> for ALL public ControllerRouteFluent NeedHttpGet_All(bool required = true) { _routes.ForEach(x => x.NeedHttpGet(required)); return this; } public ControllerRouteFluent NeedHttpPost_All(bool required = true) { _routes.ForEach(x => x.NeedHttpPost(required)); return this; } public ControllerRouteFluent WithDefaults_All(object defaults) { _routes.ForEach(x => x.WithDefaults(defaults)); return this; } public ControllerRouteFluent WithConstraints_All(object constraints) { _routes.ForEach(x => x.WithConstraints(constraints)); return this; } public ControllerRouteFluent WithNamespaces_All(string[] namespaces) { _routes.ForEach(x => x.WithNamespaces(namespaces)); return this; } public ControllerRouteFluent NeedSecurityKey_All(string securityKeyName) { _routes.ForEach(x => x.NeedSecurityKey(securityKeyName)); return this; } public ControllerRouteFluent WithDataTokens_All(object dataTokens) { _routes.ForEach(x => x.WithDataTokens(dataTokens)); return this; } public ControllerRouteFluent WithDelegate_All(Action routeFluentAction) { _routes.ForEach(x => routeFluentAction(x)); return this; } public ControllerRouteFluent JsonResultModel_All() { _routes.ForEach(x =>x.JsonResultModel()); return this; } public ControllerRouteFluent JsonResultModel() { if (_routes.Any()) _routes.Last().JsonResultModel(); return this; } #endregion } }