/// <summary> /// Initializes a new instance of the <see cref="RuntimeRoute" /> class. /// </summary> /// <param name="routeIdentifier">The route key.</param> /// <param name="methodInfo">The method information.</param> /// <param name="instanceType">Type of the instance.</param> /// <param name="instance">The instance.</param> /// <param name="isActionUsed">if set to <c>true</c> [is action used].</param> /// <param name="isTokenRequired">if set to <c>true</c> [is token required].</param> /// <param name="moduleName">Name of the module.</param> /// <param name="contentType">Type of the content.</param> /// <param name="setting">The setting.</param> /// <param name="apiCacheAttribute">The API cache attribute.</param> /// <param name="omitApiTracking">The omit API tracking.</param> /// <param name="permissions">The permissions.</param> /// <param name="headerKeys">The header keys.</param> public RuntimeRoute(ApiRouteIdentifier routeIdentifier, MethodInfo methodInfo, Type instanceType, object instance, bool isActionUsed, bool isTokenRequired, string moduleName, string contentType, RestApiSettings setting, ApiCacheAttribute apiCacheAttribute, OmitApiTrackingAttribute omitApiTracking, IDictionary <string, ApiPermissionAttribute> permissions = null, List <string> headerKeys = null) : this() { ApiMethod = methodInfo; ApiInstance = instance; IsActionUsed = isActionUsed; InstanceType = instanceType; Setting = setting; OmitApiTracking = omitApiTracking; OperationParameters = new RuntimeApiOperationParameters { ContentType = contentType, IsTokenRequired = isTokenRequired, CustomizedHeaderKeys = headerKeys, Permissions = permissions, ModuleName = moduleName }; ApiRouteIdentifier = routeIdentifier; IsVoid = ApiMethod?.ReturnType?.IsVoid(); if (apiCacheAttribute != null) { ApiCacheAttribute = apiCacheAttribute; ApiCacheContainer = apiCacheAttribute.CacheContainer ?? new ApiCacheContainer(routeIdentifier.ToString(), apiCacheAttribute.CacheParameter); } }
public ApiCacheMiddleware(RequestDelegate next) { _next = next; var assembly = Assembly.GetExecutingAssembly(); Type[] types = assembly.GetTypes(); Type[] apicontrollers = types.Where(type => type.GetCustomAttribute <ApiControllerAttribute>() != null).ToArray(); foreach (Type ctrl in apicontrollers) { MethodInfo[] controllerMethods = ctrl.GetMethods(); RouteAttribute apiRoute = ctrl.GetCustomAttribute <RouteAttribute>(); if (apiRoute == null) { throw new Exception($"{ctrl.Name} doesn't have a \"Route\" Attribute"); } string ctrRoute = apiRoute.Template; foreach (MethodInfo meth in controllerMethods) { ApiCacheAttribute apiCacheAttribute = meth.GetCustomAttribute <ApiCacheAttribute>(); if (apiCacheAttribute != null) { string route; HttpGetAttribute get = meth.GetCustomAttribute <HttpGetAttribute>(); if (get != null) { route = get.Template; string key = "/" + ctrRoute + "/" + route; if (this.ApiMap.ContainsKey(key)) { throw new Exception("Api Method Route Repeat Exception!"); } this.ApiMap.Add(key, apiCacheAttribute); } } } } }