private static Resource CreateResource(Type type, object value, HateoasOptions options, IActionDescriptorCollectionProvider actionDescriptorProvider, IUrlHelper urlHelper) { var resourceOptions = options.Requirements.Where(r => r.ResourceType == type); var isEnumerable = type.GetInterfaces().Contains(typeof(IEnumerable)); var resource = default(Resource); if (isEnumerable) { resource = new ListItemResource(value); } else { resource = new ObjectResource(value); } foreach (var option in resourceOptions) { var route = actionDescriptorProvider.ActionDescriptors.Items.FirstOrDefault(i => i.AttributeRouteInfo.Name == option.Name); var method = route.ActionConstraints.OfType <HttpMethodActionConstraint>().First().HttpMethods.First(); var routeValues = default(object); if (!isEnumerable) { routeValues = option.RouteValues(value); } var url = urlHelper.Link(option.Name, routeValues).ToLower(); resource.Links.Add(new Link(option.Name, url, method)); } return(resource); }
private Resource CreateListResource(Type type, object value) { var resource = new ListItemResource(value); return(AppendLinksToResource(type, resource, true)); }