예제 #1
0
        public static MethodModel Delete(this MethodModel model, string template = null)
        {
            model.HttpMethod = HttpMethods.Delete;

            if (template == null)
            {
                return(model);
            }

            return(model.Route(template));
        }
예제 #2
0
        public static HttpModel FromType(Type type)
        {
            var model   = new HttpModel();
            var methods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly);

            var routeAttribute = type.GetCustomAttribute <RouteAttribute>();

            foreach (var method in methods)
            {
                var attribute  = method.GetCustomAttribute <HttpMethodAttribute>();
                var httpMethod = attribute?.Method;
                var template   = CombineRoute(routeAttribute?.Template, attribute?.Template ?? method.GetCustomAttribute <RouteAttribute>()?.Template);

                var methodModel = new MethodModel
                {
                    MethodInfo = method,
                    ReturnType = method.ReturnType,
                    HttpMethod = httpMethod,
                };

                if (template != null)
                {
                    methodModel.Route(template);
                }

                foreach (var parameter in method.GetParameters())
                {
                    var fromQuery   = parameter.GetCustomAttribute <FromQueryAttribute>();
                    var fromHeader  = parameter.GetCustomAttribute <FromHeaderAttribute>();
                    var fromForm    = parameter.GetCustomAttribute <FromFormAttribute>();
                    var fromBody    = parameter.GetCustomAttribute <FromBodyAttribute>();
                    var fromRoute   = parameter.GetCustomAttribute <FromRouteAttribute>();
                    var fromCookie  = parameter.GetCustomAttribute <FromCookieAttribute>();
                    var fromService = parameter.GetCustomAttribute <FromServicesAttribute>();

                    methodModel.Parameters.Add(new ParameterModel
                    {
                        Name          = parameter.Name,
                        ParameterType = parameter.ParameterType,
                        FromQuery     = fromQuery == null ? null : fromQuery?.Name ?? parameter.Name,
                        FromHeader    = fromHeader == null ? null : fromHeader?.Name ?? parameter.Name,
                        FromForm      = fromForm == null ? null : fromForm?.Name ?? parameter.Name,
                        FromRoute     = fromRoute == null ? null : fromRoute?.Name ?? parameter.Name,
                        FromCookie    = fromCookie == null ? null : fromCookie?.Name,
                        FromBody      = fromBody != null,
                        FromServices  = fromService != null
                    });
                }

                model.Methods.Add(methodModel);
            }

            return(model);
        }