Exemplo n.º 1
0
        private static async Task <string> GetResponseMessageAsync(HttpClient httpClient, RouteInfo routeInfo, params object[] data)
        {
            HttpResponseMessage httpResponseMessage;
            StringContent       httpContent;

            PrepareConstraintParameters(out string url, out IList <string> constraintNameList, routeInfo, data);
            var httpMethod = (HttpMethod)Enum.Parse(typeof(HttpMethod), routeInfo.HttpMethods, true);

            switch (httpMethod)
            {
            case HttpMethod.Get:
                httpResponseMessage = await httpClient.GetAsync(url);

                break;

            case HttpMethod.Post:
                PrepareStringContent(out httpContent, constraintNameList, routeInfo, data);
                httpResponseMessage = await httpClient.PostAsync(url, httpContent);

                httpContent.Dispose();
                break;

            case HttpMethod.Patch:
                PrepareStringContent(out httpContent, constraintNameList, routeInfo, data);
                httpResponseMessage = await httpClient.PatchAsync(url, httpContent);

                httpContent.Dispose();
                break;

            case HttpMethod.Delete:
                httpResponseMessage = await httpClient.DeleteAsync(url);

                break;

            case HttpMethod.Put:
                PrepareStringContent(out httpContent, constraintNameList, routeInfo, data);
                httpResponseMessage = await httpClient.PutAsync(url, httpContent);

                httpContent.Dispose();
                break;

            default:
                throw new HttpRequestException($"Waiting for Support! Http Method: {httpMethod}.");
            }

            var message = await httpResponseMessage.Content.ReadAsStringAsync();

            httpResponseMessage.Dispose();
            return(message);
        }
Exemplo n.º 2
0
        private static void PrepareConstraintParameters(out string constraintUrl, out IList <string> constraintNameList, RouteInfo routeInfo, params object[] data)
        {
            var endpoint = CreateEndpoint(routeInfo.Path, routeName: routeInfo.Path);

            constraintNameList = endpoint.RoutePattern.Parameters.Select(o => o.Name).ToList();
            var urlHelper = CreateUrlHelper(new[] { endpoint });

            // Set the endpoint feature and current context just as a normal request to MVC app would be
            var endpointFeature = new EndpointSelectorContext();

            urlHelper.ActionContext.HttpContext.Features.Set <IEndpointFeature>(endpointFeature);
            urlHelper.ActionContext.HttpContext.Features.Set <IRouteValuesFeature>(endpointFeature);
            endpointFeature.Endpoint    = endpoint;
            endpointFeature.RouteValues = new RouteValueDictionary();
            foreach (var item in routeInfo.Parameters)
            {
                int    dataIndex = routeInfo.Parameters.IndexOf(item);
                object value     = data.ElementAt(dataIndex);
                endpointFeature.RouteValues.TryAdd(item.Name, value);
            }

            constraintUrl = urlHelper.RouteUrl(routeName: routeInfo.Path, values: endpointFeature.RouteValues);
        }
Exemplo n.º 3
0
        private static void PrepareStringContent(out StringContent httpContent, IList <string> constraintNameList, RouteInfo routeInfo, params object[] data)
        {
            object stringContent = new object();

            if (constraintNameList.Count == 0)
            {
                stringContent = data[0];
            }
            else
            {
                for (int i = 0; i < constraintNameList.Count; i++)
                {
                    var index = routeInfo.Parameters.IndexOf(routeInfo.Parameters.FirstOrDefault(o => o.Name != constraintNameList[i]));
                    stringContent = data[index];
                }
            }

            string content = JsonConvert.SerializeObject(stringContent);

            httpContent = new StringContent(content);
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");
        }
Exemplo n.º 4
0
        public IList <RouteInfo> GetAllRouteInfo()
        {
            List <RouteInfo> list = new List <RouteInfo>();

            var routeCollection = this.actionDescriptorCollectionProvider.ActionDescriptors.Items;

            foreach (ActionDescriptor route in routeCollection)
            {
                RouteInfo info = new RouteInfo();

                // Area
                if (route.RouteValues.ContainsKey("area"))
                {
                    info.Area = route.RouteValues["area"];
                }

                // Path and Invocation of Razor Pages
                if (route is PageActionDescriptor)
                {
                    var e = route as PageActionDescriptor;
                    info.Path      = e.ViewEnginePath;
                    info.Namespace = e.RelativePath;
                }

                // Path of Route Attribute
                if (route.AttributeRouteInfo != null)
                {
                    var e = route;
                    info.Path = $"/{e.AttributeRouteInfo.Template}";
                    this.AddParameters(info, e);
                }

                if (route is ControllerActionDescriptor)
                {
                    var e = route as ControllerActionDescriptor;
                    info.ControllerName = e.ControllerName;
                    info.ActionName     = e.ActionName;

                    info.Namespace = e.ControllerTypeInfo.AsType().Namespace;
                    if (string.IsNullOrEmpty(e.AttributeRouteInfo?.Template))
                    {
                        if (!string.IsNullOrEmpty(info.Area))
                        {
                            info.Path = $"/{info.Area}";
                        }

                        info.Path += $"/{e.ControllerName}/{e.ActionName}";
                    }

                    this.AddParameters(info, e);
                }

                // Extract HTTP Verb
                if (route.ActionConstraints != null && route.ActionConstraints.Select(t => t.GetType()).Contains(typeof(HttpMethodActionConstraint)))
                {
                    if (route.ActionConstraints.FirstOrDefault(a => a.GetType() == typeof(HttpMethodActionConstraint)) is HttpMethodActionConstraint httpMethodAction)
                    {
                        info.HttpMethods = string.Join(",", httpMethodAction.HttpMethods);
                    }
                }

                list.Add(info);
            }

            return(list);
        }