public async Task BeforeRequestAsyncTest()
        {
            var context = new ApiActionContext
            {
                RequestMessage = new HttpApiRequestMessage
                {
                    RequestUri = new Uri("http://www.webapi.com/")
                },
                ApiActionDescriptor = ApiDescriptorCache.GetApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync"))
            };

            var attr = new HttpPutAttribute();
            await attr.BeforeRequestAsync(context);

            Assert.True(context.RequestMessage.Method == HttpMethod.Put);

            var attr2 = new HttpPutAttribute("/login");
            await attr2.BeforeRequestAsync(context);

            Assert.True(context.RequestMessage.Method == HttpMethod.Put);
            Assert.True(context.RequestMessage.RequestUri == new Uri("http://www.webapi.com/login"));

            var attr3 = new HttpPutAttribute("http://www.baidu.com");
            await attr3.BeforeRequestAsync(context);

            Assert.True(context.RequestMessage.Method == HttpMethod.Put);
            Assert.True(context.RequestMessage.RequestUri == new Uri("http://www.baidu.com"));
        }
Beispiel #2
0
        public async Task OnRequestAsyncTest()
        {
            var apiAction = new ApiActionDescriptor(typeof(ITestApi).GetMethod("PostAsync"));
            var context   = new TestRequestContext(apiAction, string.Empty);

            context.HttpContext.RequestMessage.RequestUri = new Uri("http://www.webapi.com/");
            context.HttpContext.RequestMessage.Method     = HttpMethod.Post;

            var attr = new HttpPutAttribute();
            await attr.OnRequestAsync(context);

            Assert.True(context.HttpContext.RequestMessage.Method == HttpMethod.Put);

            var attr2 = new HttpPutAttribute("/login");
            await attr2.OnRequestAsync(context);

            Assert.True(context.HttpContext.RequestMessage.Method == HttpMethod.Put);
            Assert.True(context.HttpContext.RequestMessage.RequestUri == new Uri("http://www.webapi.com/login"));

            var attr3 = new HttpPutAttribute("http://www.baidu.com");
            await attr3.OnRequestAsync(context);

            Assert.True(context.HttpContext.RequestMessage.Method == HttpMethod.Put);
            Assert.True(context.HttpContext.RequestMessage.RequestUri == new Uri("http://www.baidu.com"));
        }
Beispiel #3
0
        public async Task BeforeRequestAsyncTest()
        {
            var context = new TestActionContext(
                httpApi: null,
                httpApiConfig: new HttpApiConfig(),
                apiActionDescriptor: new ApiActionDescriptor(typeof(IMyApi).GetMethod("PostAsync")));

            context.RequestMessage.RequestUri = new Uri("http://www.webapi.com/");
            context.RequestMessage.Method     = HttpMethod.Post;

            var attr = new HttpPutAttribute();
            await attr.BeforeRequestAsync(context);

            Assert.True(context.RequestMessage.Method == HttpMethod.Put);

            var attr2 = new HttpPutAttribute("/login");
            await attr2.BeforeRequestAsync(context);

            Assert.True(context.RequestMessage.Method == HttpMethod.Put);
            Assert.True(context.RequestMessage.RequestUri == new Uri("http://www.webapi.com/login"));

            var attr3 = new HttpPutAttribute("http://www.baidu.com");
            await attr3.BeforeRequestAsync(context);

            Assert.True(context.RequestMessage.Method == HttpMethod.Put);
            Assert.True(context.RequestMessage.RequestUri == new Uri("http://www.baidu.com"));
        }
        /// <summary>
        /// Resolves any Mvc attributes on the method.
        /// </summary>
        /// <param name="methodBuilder">The method being built.</param>
        /// <param name="methodInfo">The method being called.</param>
        /// <returns>True if any resolved; otherwise false.</returns>
        public static bool ResolveMvcAttributes(
            this IMethodBuilder methodBuilder,
            MethodInfo methodInfo)
        {
            HttpGetAttribute getAttr = methodInfo.GetCustomAttribute <HttpGetAttribute>(true);

            if (getAttr != null)
            {
                methodBuilder.SetCustomAttribute(AttributeUtility.BuildAttribute <string, HttpGetAttribute>(getAttr.Template));
                return(true);
            }

            HttpPostAttribute postAttr = methodInfo.GetCustomAttribute <HttpPostAttribute>(true);

            if (postAttr != null)
            {
                methodBuilder.SetCustomAttribute(AttributeUtility.BuildAttribute <string, HttpPostAttribute>(postAttr.Template));
                return(true);
            }

            HttpPutAttribute putAttr = methodInfo.GetCustomAttribute <HttpPutAttribute>(true);

            if (putAttr != null)
            {
                methodBuilder.SetCustomAttribute(AttributeUtility.BuildAttribute <string, HttpPutAttribute>(putAttr.Template));
                return(true);
            }

            HttpPatchAttribute patchAttr = methodInfo.GetCustomAttribute <HttpPatchAttribute>(true);

            if (patchAttr != null)
            {
                methodBuilder.SetCustomAttribute(AttributeUtility.BuildAttribute <string, HttpPatchAttribute>(patchAttr.Template));
                return(true);
            }

            HttpDeleteAttribute deleteAttr = methodInfo.GetCustomAttribute <HttpDeleteAttribute>(true);

            if (deleteAttr != null)
            {
                methodBuilder.SetCustomAttribute(AttributeUtility.BuildAttribute <string, HttpDeleteAttribute>(deleteAttr.Template));
                return(true);
            }

            return(false);
        }
Beispiel #5
0
        /// <summary>
        /// Defines the request type of a controller method
        /// </summary>
        /// <param name="controllerName"></param>
        /// <param name="methodName"></param>
        /// <param name="methodAttributes"></param>
        /// <returns></returns>
        public static (RequestType, object) GetRequestType(string controllerName, string methodName, object[] methodAttributes)
        {
            try
            {
                var httpGetAttrCount    = methodAttributes.Count(x => x.GetType() == typeof(HttpGetAttribute));
                var httpPostAttrCount   = methodAttributes.Count(x => x.GetType() == typeof(HttpPostAttribute));
                var httpPutAttrCount    = methodAttributes.Count(x => x.GetType() == typeof(HttpPutAttribute));
                var httpDeleteAttrCount = methodAttributes.Count(x => x.GetType() == typeof(HttpDeleteAttribute));
                if ((httpGetAttrCount + httpPostAttrCount + httpPutAttrCount + httpDeleteAttrCount) > 1)
                {
                    throw new Exception($"Weber controller Error: Controller: {controllerName} => Method: {methodName} => Cannot have more than one request type attribute");
                }
                foreach (var attribute in methodAttributes)
                {
                    if (HttpGetAttribute.IsHttpGet(attribute))
                    {
                        return(RequestType.GET, attribute);
                    }

                    if (HttpPostAttribute.IsHttpPost(attribute))
                    {
                        return(RequestType.POST, attribute);
                    }

                    if (HttpPutAttribute.IsHttpPut(attribute))
                    {
                        return(RequestType.PUT, attribute);
                    }

                    if (HttpDeleteAttribute.IsHttpDelete(attribute))
                    {
                        return(RequestType.DELETE, attribute);
                    }
                }
            }
            catch (Exception e)
            {
                throw new Exception($"RequestTypeHelper GetRequestType Error: {e.Message}");
            }
            return(RequestType.None, null);
        }
Beispiel #6
0
        public async Task <ActionResult> GetController()
        {
            List <xQuyen> lstQuyens = new List <xQuyen>();

            Assembly asm = Assembly.GetExecutingAssembly();

            var q1 = asm
                     .GetExportedTypes()
                     .Where(x => typeof(CustomController).IsAssignableFrom(x) && !x.Name.Equals(typeof(CustomController).Name))
                     .Select(x => new
            {
                Controller = x.Name,
                Methods    = x.GetMethods().Where(y => y.DeclaringType.IsSubclassOf(typeof(CustomController)) && y.IsPublic && !y.IsStatic).ToList()
            });

            var BaseController = q1
                                 .Where(x => x.Controller.Equals(typeof(BaseController <>).Name))
                                 .Select(x => new
            {
                Controller = x.Controller.ToLower().Replace("controller", string.Empty),
                Actions    = x.Methods.Where(y => y.IsVirtual).Select(y => new { Action = y.Name.ToLower(), Attributes = y.GetCustomAttributes(false).ToList() })
            })
                                 .FirstOrDefault();

            var Controllers = q1
                              .Where(x => !x.Controller.Equals(typeof(BaseController <>).Name))
                              .Select(x => new
            {
                Controller = x.Controller.ToLower().Replace("controller", string.Empty),
                Actions    = x.Methods.Where(y => !y.IsVirtual).Select(y => new { Action = y.Name.ToLower(), Attributes = y.GetCustomAttributes(false).ToList() })
            });

            DateTime time = DateTime.Now;

            if (BaseController != null)
            {
                foreach (var action in BaseController.Actions)
                {
                    xQuyen quyen = new xQuyen();

                    HttpGetAttribute attr_Get   = (HttpGetAttribute)action.Attributes.FirstOrDefault(x => x.GetType() == typeof(HttpGetAttribute));
                    RouteAttribute   attr_Route = (RouteAttribute)action.Attributes.FirstOrDefault(x => x.GetType() == typeof(RouteAttribute));
                    if (attr_Get != null && attr_Route != null)
                    {
                        quyen.Method   = HttpVerbs.Get.ToString().ToLower();
                        quyen.Template = string.IsNullOrWhiteSpace(attr_Route.Template) ? string.Empty : attr_Route.Template.ToLower();
                        lstQuyens.Add(quyen);
                    }
                    else if (attr_Get != null && attr_Route == null)
                    {
                        quyen.Method = HttpVerbs.Get.ToString().ToLower();
                        lstQuyens.Add(quyen);
                    }
                    else if (attr_Get == null && attr_Route != null)
                    {
                        quyen.Method   = HttpVerbs.Get.ToString().ToLower();
                        quyen.Template = string.IsNullOrWhiteSpace(attr_Route.Template) ? string.Empty : attr_Route.Template.ToLower();
                        lstQuyens.Add(quyen);
                    }

                    HttpPostAttribute attr_Post = (HttpPostAttribute)action.Attributes.FirstOrDefault(x => x.GetType() == typeof(HttpPostAttribute));
                    if (attr_Post != null)
                    {
                        quyen.Method = HttpVerbs.Post.ToString().ToLower();
                        lstQuyens.Add(quyen);
                    }

                    HttpPutAttribute attr_Put = (HttpPutAttribute)action.Attributes.FirstOrDefault(x => x.GetType() == typeof(HttpPutAttribute));
                    if (attr_Put != null)
                    {
                        quyen.Method = HttpVerbs.Put.ToString().ToLower();
                        lstQuyens.Add(quyen);
                    }

                    HttpDeleteAttribute attr_Delete = (HttpDeleteAttribute)action.Attributes.FirstOrDefault(x => x.GetType() == typeof(HttpDeleteAttribute));
                    if (attr_Delete != null)
                    {
                        quyen.Method = HttpVerbs.Delete.ToString().ToLower();
                        lstQuyens.Add(quyen);
                    }



                    quyen.KeyID      = 0;
                    quyen.NgayTao    = time;
                    quyen.Controller = BaseController.Controller;
                    quyen.Action     = action.Action;
                    quyen.MacDinh    = true;
                    quyen.Path       = string.Join("/", quyen.Controller, quyen.Action, quyen.Template).TrimEnd('/');
                }
            }

            foreach (var controller in Controllers)
            {
                List <xQuyen> lstTemps = new List <xQuyen>();

                foreach (var action in controller.Actions)
                {
                    xQuyen f = new xQuyen();

                    HttpGetAttribute attr_Get = (HttpGetAttribute)action.Attributes.FirstOrDefault(x => x.GetType() == typeof(HttpGetAttribute));
                    if (attr_Get != null)
                    {
                        f.Method = HttpVerbs.Get.ToString().ToLower();
                        // f.Template = string.IsNullOrWhiteSpace(attr_Get.Template) ? string.Empty : attr_Get.Template.ToLower();
                        lstTemps.Add(f);
                    }

                    HttpPostAttribute attr_Post = (HttpPostAttribute)action.Attributes.FirstOrDefault(x => x.GetType() == typeof(HttpPostAttribute));
                    if (attr_Post != null)
                    {
                        f.Method = HttpVerbs.Post.ToString().ToLower();
                        //f.Template = string.IsNullOrWhiteSpace(attr_Post.Template) ? string.Empty : attr_Post.Template.ToLower();
                        lstTemps.Add(f);
                    }

                    HttpPutAttribute attr_Put = (HttpPutAttribute)action.Attributes.FirstOrDefault(x => x.GetType() == typeof(HttpPutAttribute));
                    if (attr_Put != null)
                    {
                        f.Method = HttpVerbs.Put.ToString().ToLower();
                        // f.Template = string.IsNullOrWhiteSpace(attr_Put.Template) ? string.Empty : attr_Put.Template.ToLower();
                        lstTemps.Add(f);
                    }

                    HttpDeleteAttribute attr_Delete = (HttpDeleteAttribute)action.Attributes.FirstOrDefault(x => x.GetType() == typeof(HttpDeleteAttribute));
                    if (attr_Delete != null)
                    {
                        f.Method = HttpVerbs.Delete.ToString().ToLower();
                        //  f.Template = string.IsNullOrWhiteSpace(attr_Delete.Template) ? string.Empty : attr_Delete.Template.ToLower();
                        lstTemps.Add(f);
                    }

                    RouteAttribute attr_Route = (RouteAttribute)action.Attributes.FirstOrDefault(x => x.GetType() == typeof(RouteAttribute));
                    if (attr_Route != null)
                    {
                        f.Method   = string.IsNullOrWhiteSpace(f.Method) ? HttpVerbs.Get.ToString().ToLower() : f.Method;
                        f.Template = string.IsNullOrWhiteSpace(attr_Route.Template) ? string.Empty : attr_Route.Template.ToLower();
                        lstTemps.Add(f);
                    }

                    f.KeyID      = 0;
                    f.NgayTao    = time;
                    f.Controller = controller.Controller;
                    f.Action     = action.Action;
                    f.Path       = string.Join("/", f.Controller, f.Action, f.Template).TrimEnd('/');
                }

                lstQuyens.AddRange(lstTemps);
            }

            return(await SaveData(lstQuyens.ToArray()));
        }
Beispiel #7
0
        public void Apply(ActionModel action)
        {
            var method = action.ActionMethod;

            if (method.IsDefined(typeof(HttpMethodAttribute)))
            {
                return;
            }
            action.Selectors.Clear();
            //custom prefix
            HttpMethodAttribute attr;

            foreach (var custom in _options.CustomRule)
            {
                if (action.ActionName.StartsWith(custom.Key))
                {
                    switch (custom.Value)
                    {
                    case HttpVerbs.HttpGet:
                        attr = new HttpGetAttribute(action.ActionName);
                        break;

                    case HttpVerbs.HttpPost:
                        attr = new HttpPostAttribute(action.ActionName);
                        break;

                    case HttpVerbs.HttpDelete:
                        attr = new HttpDeleteAttribute(action.ActionName);
                        break;

                    case HttpVerbs.HttpPut:
                        attr = new HttpPutAttribute(action.ActionName);
                        break;

                    default:
                        attr = new HttpPostAttribute(action.ActionName);
                        break;
                    }
                    ModelConventionHelper.AddRange(action.Selectors, ModelConventionHelper.CreateSelectors(new List <object> {
                        attr
                    }));
                    return;
                }
            }

            if (method.Name.StartsWith("Get"))
            {
                attr = new HttpGetAttribute(method.Name);
            }
            else if (method.Name.StartsWith("Post") || method.Name.StartsWith("Add"))
            {
                attr = new HttpPostAttribute(method.Name);
            }
            else if (method.Name.StartsWith("Update") || method.Name.StartsWith("Put"))
            {
                attr = new HttpPutAttribute(method.Name);
            }
            else if (method.Name.StartsWith("Del") || method.Name.StartsWith("Delete"))
            {
                attr = new HttpDeleteAttribute(method.Name);
            }
            else
            {
                attr = new HttpPostAttribute(method.Name);
            }
            ModelConventionHelper.AddRange(action.Selectors, ModelConventionHelper.CreateSelectors(new List <object> {
                attr
            }));
        }
Beispiel #8
0
        public async Task <IActionResult> GetController()
        {
            List <xFeature> lstFeatures = new List <xFeature>();

            Assembly asm = Assembly.GetExecutingAssembly();

            var Controllers = asm.GetExportedTypes()
                              .Where(x => typeof(ControllerBase).IsAssignableFrom(x) && !x.Name.Equals(typeof(BaseController <>).Name))
                              .Select(x => new
            {
                Controller = x.Name,
                Methods    = x.GetMethods().Where(y => y.DeclaringType.IsSubclassOf(typeof(ControllerBase)) && y.IsPublic && !y.IsStatic).ToList()
            })
                              .Select(x => new
            {
                Controller = x.Controller.ToLower().Replace("controller", string.Empty),
                Actions    = x.Methods.Select(y => new { Action = y.Name.ToLower(), Attributes = y.GetCustomAttributes(true).ToList() })
            });

            DateTime time = DateTime.Now;

            foreach (var controller in Controllers)
            {
                List <xFeature> lstTemps = new List <xFeature>();

                foreach (var action in controller.Actions)
                {
                    xFeature f = new xFeature();

                    HttpGetAttribute attr_Get = (HttpGetAttribute)action.Attributes.FirstOrDefault(x => x.GetType() == typeof(HttpGetAttribute));
                    if (attr_Get != null)
                    {
                        f.Method   = HttpMethods.Get.ToLower();
                        f.Template = string.IsNullOrWhiteSpace(attr_Get.Template) ? string.Empty : attr_Get.Template.ToLower();
                        lstTemps.Add(f);
                    }

                    HttpPostAttribute attr_Post = (HttpPostAttribute)action.Attributes.FirstOrDefault(x => x.GetType() == typeof(HttpPostAttribute));
                    if (attr_Post != null)
                    {
                        f.Method   = HttpMethods.Post.ToLower();
                        f.Template = string.IsNullOrWhiteSpace(attr_Post.Template) ? string.Empty : attr_Post.Template.ToLower();
                        lstTemps.Add(f);
                    }

                    HttpPutAttribute attr_Put = (HttpPutAttribute)action.Attributes.FirstOrDefault(x => x.GetType() == typeof(HttpPutAttribute));
                    if (attr_Put != null)
                    {
                        f.Method   = HttpMethods.Put.ToLower();
                        f.Template = string.IsNullOrWhiteSpace(attr_Put.Template) ? string.Empty : attr_Put.Template.ToLower();
                        lstTemps.Add(f);
                    }

                    HttpDeleteAttribute attr_Delete = (HttpDeleteAttribute)action.Attributes.FirstOrDefault(x => x.GetType() == typeof(HttpDeleteAttribute));
                    if (attr_Delete != null)
                    {
                        f.Method   = HttpMethods.Delete.ToLower();
                        f.Template = string.IsNullOrWhiteSpace(attr_Delete.Template) ? string.Empty : attr_Delete.Template.ToLower();
                        lstTemps.Add(f);
                    }

                    RouteAttribute attr_Route = (RouteAttribute)action.Attributes.FirstOrDefault(x => x.GetType() == typeof(RouteAttribute));
                    if (attr_Route != null)
                    {
                        f.Method   = string.IsNullOrWhiteSpace(f.Method) ? HttpMethods.Get.ToLower() : f.Method;
                        f.Template = string.IsNullOrWhiteSpace(attr_Route.Template) ? string.Empty : attr_Route.Template.ToLower();
                        lstTemps.Add(f);
                    }

                    f.KeyID      = 0;
                    f.NgayTao    = time;
                    f.Controller = controller.Controller;
                    f.Action     = action.Action;
                    f.Path       = string.Join('/', "api", f.Controller, f.Template).TrimEnd('/');
                }

                lstFeatures.AddRange(lstTemps);
            }

            return(await SaveData(lstFeatures.ToArray()));
        }