public ContentResult GetAllRoutes()
        {
            StringBuilder nonSb = new StringBuilder();
            StringBuilder sb    = new StringBuilder();

            foreach (var endpoint in _dataSource.Endpoints)
            {
                ControllerActionDescriptor controllerActionDescriptor = endpoint.Metadata.GetMetadata <ControllerActionDescriptor>();
                if (controllerActionDescriptor == null)
                {
                    continue;
                }

                IODataRoutingMetadata metadata = endpoint.Metadata.GetMetadata <IODataRoutingMetadata>();
                if (metadata == null)
                {
                    AppendRoute(nonSb, endpoint);
                }
                else
                {
                    AppendRoute(sb, endpoint);
                }
            }

            string output = ODataRouteMappingHtmlTemplate.Replace("ODATACONTENT", sb.ToString(), StringComparison.OrdinalIgnoreCase);

            output = output.Replace("NONENDPOINTCONTENT", nonSb.ToString(), StringComparison.OrdinalIgnoreCase);

            return(base.Content(output, "text/html"));
        }
            public ContentResult GetAllRoutes()
            {
                StringBuilder nonSb = new StringBuilder();
                StringBuilder sb    = new StringBuilder();

                foreach (var endpoint in _dataSource.Endpoints)
                {
                    ControllerActionDescriptor controllerActionDescriptor = endpoint.Metadata.GetMetadata <ControllerActionDescriptor>();
                    if (controllerActionDescriptor == null)
                    {
                        continue;
                    }

                    IODataRoutingMetadata metadata = endpoint.Metadata.GetMetadata <IODataRoutingMetadata>();
                    if (metadata == null)
                    {
                        AppendNonODataRoute(nonSb, endpoint);
                        continue;
                    }

                    // controller and action details
                    StringBuilder action = new StringBuilder();
                    if (controllerActionDescriptor.MethodInfo.ReturnType != null)
                    {
                        action.Append(controllerActionDescriptor.MethodInfo.ReturnType.Name + " ");
                    }
                    else
                    {
                        action.Append("void ");
                    }
                    action.Append(controllerActionDescriptor.MethodInfo.Name + "(");
                    action.Append(string.Join(",", controllerActionDescriptor.MethodInfo.GetParameters().Select(p => p.ParameterType.Name)));
                    action.Append(")");
                    string actionName = controllerActionDescriptor.MethodInfo.Name;

                    sb.Append("<tr>");
                    sb.Append($"<td>{GetActionDesciption(controllerActionDescriptor)}</td>");

                    // http methods
                    string httpMethods = string.Join(",", metadata.HttpMethods);
                    sb.Append($"<td>{httpMethods.ToUpper()}</td>");

                    // template name
                    RouteEndpoint routeEndpoint = endpoint as RouteEndpoint;
                    if (routeEndpoint != null)
                    {
                        sb.Append("<td>~/").Append(routeEndpoint.RoutePattern.RawText).Append("</td></tr>");
                    }
                    else
                    {
                        sb.Append("<td>---NON RouteEndpoint---</td></tr>");
                    }
                }

                string output = ODataRouteMappingHtmlTemplate.Replace("{CONTENT}", sb.ToString());

                output = output.Replace("{NONENDPOINTCONTENT}", nonSb.ToString());

                return(base.Content(output, "text/html"));
            }
Пример #3
0
        internal async Task HandleEndpointsAsync(HttpContext context)
        {
            if (context == null)
            {
                throw Error.ArgumentNull(nameof(context));
            }

            StringBuilder      nonSb      = new StringBuilder();
            StringBuilder      sb         = new StringBuilder();
            EndpointDataSource dataSource = context.RequestServices.GetRequiredService <EndpointDataSource>();

            foreach (var endpoint in dataSource.Endpoints)
            {
                ControllerActionDescriptor controllerActionDescriptor = endpoint.Metadata.GetMetadata <ControllerActionDescriptor>();
                if (controllerActionDescriptor == null)
                {
                    continue;
                }

                IODataRoutingMetadata metadata = endpoint.Metadata.GetMetadata <IODataRoutingMetadata>();
                if (metadata == null)
                {
                    AppendRoute(nonSb, endpoint);
                }
                else
                {
                    AppendRoute(sb, endpoint);
                }
            }

            string output = ODataRouteMappingHtmlTemplate.Replace("ODATACONTENT", sb.ToString(), StringComparison.Ordinal);

            output = output.Replace("NONENDPOINTCONTENT", nonSb.ToString(), StringComparison.Ordinal);

            context.Response.Headers["Content_Type"] = "text/html";
            await context.Response.WriteAsync(output).ConfigureAwait(false);
        }
        /// <summary>
        /// Handle ~/$odata request
        /// </summary>
        /// <param name="context">The http context.</param>
        /// <returns>The task.</returns>
        public static async Task HandleOData(HttpContext context)
        {
            EndpointDataSource dataSource = context.RequestServices.GetRequiredService <EndpointDataSource>();

            StringBuilder sb = new StringBuilder();

            foreach (var endpoint in dataSource.Endpoints)
            {
                IODataRoutingMetadata metadata = endpoint.Metadata.GetMetadata <IODataRoutingMetadata>();
                if (metadata == null)
                {
                    continue;
                }

                ControllerActionDescriptor controllerActionDescriptor = endpoint.Metadata.GetMetadata <ControllerActionDescriptor>();
                if (controllerActionDescriptor == null)
                {
                    continue;
                }

                // controller and action details
                StringBuilder action = new StringBuilder();
                if (controllerActionDescriptor.MethodInfo.ReturnType != null)
                {
                    action.Append(controllerActionDescriptor.MethodInfo.ReturnType.Name + " ");
                }
                else
                {
                    action.Append("void ");
                }
                action.Append(controllerActionDescriptor.MethodInfo.Name + "(");
                action.Append(string.Join(",", controllerActionDescriptor.MethodInfo.GetParameters().Select(p => p.ParameterType.Name)));
                action.Append(")");
                string actionName = controllerActionDescriptor.MethodInfo.Name;

                sb.Append("<tr>");
                sb.Append($"<td>{controllerActionDescriptor.ControllerTypeInfo.FullName}</td>");
                sb.Append($"<td>{action}</td>");

                // http methods
                string httpMethods = string.Join(",", metadata.HttpMethods);
                sb.Append($"<td>{httpMethods.ToUpper()}</td>");

                // OData routing templates
                RouteEndpoint routeEndpoint = endpoint as RouteEndpoint;
                if (routeEndpoint != null)
                {
                    sb.Append("<td>~/").Append(routeEndpoint.RoutePattern.RawText).Append("</td></tr>");
                }
                else
                {
                    int index = 1;
                    sb.Append("<td>");
                    foreach (var template in metadata.Template.GetTemplates())
                    {
                        sb.Append($"{index++})");
                        if (string.IsNullOrEmpty(metadata.Prefix))
                        {
                            sb.Append(" ~/");
                        }
                        else
                        {
                            sb.Append($" ~/{metadata.Prefix}/");
                        }

                        sb.Append(template).Append("<br/>");
                    }
                    sb.Append("</td></tr>");
                }
            }

            string output = ODataRouteMappingHtmlTemplate.Replace("{CONTNET}", sb.ToString());

            context.Response.Headers["Content_Type"] = "text/html";

            await context.Response.WriteAsync(output);

            //string content = sb.ToString();
            //byte[] requestBytes = Encoding.UTF8.GetBytes(content);
            //context.Response.Headers.ContentLength = requestBytes.Length;
            //context.Response.Body = new MemoryStream(requestBytes);
            //await Task.CompletedTask;
        }