コード例 #1
0
        protected internal IDictionary <string, IList <MappingDescription> > GetMappingDescriptions(Collection <ApiDescription> apiDescriptors)
        {
            // The apiDescriptors should have descriptors for both attribute and conventional WebAPI routes
            IDictionary <string, IList <MappingDescription> > mappingDescriptions = new Dictionary <string, IList <MappingDescription> >();

            foreach (var desc in apiDescriptors)
            {
                var adesc   = desc.ActionDescriptor as ReflectedHttpActionDescriptor;
                var details = GetRouteDetails(desc);

                mappingDescriptions.TryGetValue(adesc.ControllerDescriptor.ControllerType.FullName, out IList <MappingDescription> mapList);
                if (mapList == null)
                {
                    mapList = new List <MappingDescription>();
                    mappingDescriptions.Add(adesc.ControllerDescriptor.ControllerType.FullName, mapList);
                }

                var mapDesc = new MappingDescription(adesc.MethodInfo, details);
                mapList.Add(mapDesc);
            }

            return(mappingDescriptions);
        }
コード例 #2
0
        public void JsonSerialization_ReturnsExpected()
        {
            var routeDetail = new TestRouteDetails()
            {
                HttpMethods = new List <string>()
                {
                    "GET"
                },
                RouteTemplate = "/Home/Index",
                Consumes      = new List <string>()
                {
                    "application/json"
                },
                Produces = new List <string>()
                {
                    "application/json"
                }
            };
            var mapDesc = new MappingDescription("foobar", routeDetail);

            var result = Serialize(mapDesc);

            Assert.Equal("{\"handler\":\"foobar\",\"predicate\":\"{[/Home/Index],methods=[GET],produces=[application/json],consumes=[application/json]}\"}", result);
        }
コード例 #3
0
        public void Constructor_SetsValues()
        {
            var routeDetail = new TestRouteDetails()
            {
                HttpMethods = new List <string>()
                {
                    "GET"
                },
                RouteTemplate = "/Home/Index",
                Consumes      = new List <string>()
                {
                    "application/json"
                },
                Produces = new List <string>()
                {
                    "application/json"
                }
            };
            var mapDesc = new MappingDescription("foobar", routeDetail);

            Assert.Null(mapDesc.Details);
            Assert.Equal("foobar", mapDesc.Handler);
            Assert.Equal("{[/Home/Index],methods=[GET],produces=[application/json],consumes=[application/json]}", mapDesc.Predicate);
        }
コード例 #4
0
        protected internal void AddRouteMappingsDescriptions(RouteCollection routes, IDictionary <string, IList <MappingDescription> > desc)
        {
            if (routes == null)
            {
                return;
            }

            // The RouteCollection will contain routes for everything (WebAPI, MVC, etc).
            // Since we already processed WebAPI routes, we try to ignore those as we process each route
            foreach (var router in routes)
            {
                var route = router as Route;
                if (route != null)
                {
                    var details = GetRouteDetails(route);

                    // If we are able to get an ActionDescriptor from this route
                    // then it is a MVC route that is based on attributes and we can extract the controller
                    // and controller method the route is tied to
                    var actionDesc = TryGetActionDescriptor(route);
                    if (actionDesc != null)
                    {
                        var refActionDesc = GetReflectedActionDescription(actionDesc);
                        if (refActionDesc != null)
                        {
                            var attrs = refActionDesc.GetCustomAttributes(false);
                            details.HttpMethods = GetHttpMethods(attrs);

                            desc.TryGetValue(refActionDesc.ControllerDescriptor.ControllerType.FullName, out IList <MappingDescription> mapList);

                            if (mapList == null)
                            {
                                mapList = new List <MappingDescription>();
                                desc.Add(refActionDesc.ControllerDescriptor.ControllerType.FullName, mapList);
                            }

                            var mapDesc = new MappingDescription(refActionDesc.MethodInfo, details);
                            mapList.Add(mapDesc);
                        }
                    }
                    else
                    {
                        var handler = route.RouteHandler;
                        if (handler != null)
                        {
                            // Ignore WebApi handler routes as the ApiExplorer already provided those mappings
                            if (!(handler is HttpControllerRouteHandler))
                            {
                                var handlerType = handler.GetType().ToString();
                                desc.TryGetValue(handlerType, out IList <MappingDescription> mapList);

                                if (mapList == null)
                                {
                                    mapList = new List <MappingDescription>();
                                    desc.Add(handlerType, mapList);
                                }

                                var mapDesc = new MappingDescription("IHttpHandler.ProcessRequest(HttpContext context)", details);
                                mapList.Add(mapDesc);
                            }
                        }
                    }
                }
            }
        }