public static void Configure(HttpConfiguration config)
        {
            config.EnsureInitialized();

            var apiExplorer = config.Services.GetApiExplorer();

            var table = new LinkTable();
            foreach (var apiDescription in apiExplorer.ApiDescriptions)
            {
                var descriptor = apiDescription.ActionDescriptor as ReflectedHttpActionDescriptor;
                if (descriptor != null)
                {
                    var linksFromAttributes = descriptor.MethodInfo.GetCustomAttributes(typeof (LinksFromAttribute), false).Cast<LinksFromAttribute>().ToArray();
                    if (linksFromAttributes.Length > 0)
                    {
                        foreach (var attribute in linksFromAttributes)
                        {
                            var linkBuilder = LinkBuilder.Create(attribute, apiDescription);
                            if (linkBuilder != null)
                            {
                                table.Add(linkBuilder);
                            }
                        }
                    }
                }
            }

            var json = config.Formatters.JsonFormatter;
            json.SerializerSettings.ContractResolver = new LinkyContractResolver(table, json.SerializerSettings.ContractResolver);
        }
Example #2
0
        public static void Configure(HttpConfiguration config, string propName = null)
        {
            config.EnsureInitialized();

            var apiExplorer = config.Services.GetApiExplorer();

            var table = new LinkTable();

            foreach (var apiDescription in apiExplorer.ApiDescriptions)
            {
                var descriptor = apiDescription.ActionDescriptor as ReflectedHttpActionDescriptor;
                if (descriptor != null)
                {
                    var linksFromAttributes = descriptor.MethodInfo.GetCustomAttributes(typeof(LinksFromAttribute), false).Cast <LinksFromAttribute>().ToArray();
                    if (linksFromAttributes.Length > 0)
                    {
                        foreach (var attribute in linksFromAttributes)
                        {
                            var linkBuilder = LinkBuilder.Create(attribute, apiDescription);
                            if (linkBuilder != null)
                            {
                                table.Add(linkBuilder);
                            }
                        }
                    }
                }
            }

            var json = config.Formatters.JsonFormatter;

            json.SerializerSettings.ContractResolver = new LinkyContractResolver(table, json.SerializerSettings.ContractResolver, propName);
        }
Example #3
0
 public LinkyContractResolver(LinkTable table, IContractResolver contractResolver, string propName)
 {
     _table            = table;
     _propName         = propName ?? "_links";
     _contractResolver = contractResolver ?? new DefaultContractResolver();
 }
 public LinkyContractResolver(LinkTable table, IContractResolver contractResolver)
 {
     _table = table;
     _contractResolver = contractResolver ?? new DefaultContractResolver();
 }