Example #1
0
        /// <inheritdoc />
        public Task <string> GenerateAsync(ILinkResource resource, LinkDefinition definition)
        {
            Func <ILinkResource, dynamic> valuesFunc = definition.RouteValues ?? DefaultValues;
            object values = valuesFunc(resource) as object;

            ActionContext actionContext = actionContextAccessor.ActionContext;
            IUrlHelper    helper        = urlHelperFactory.GetUrlHelper(actionContext);
            string        path          = helper.RouteUrl(definition.RouteName, values);

            return(Task.FromResult(path));
        }
Example #2
0
 /// <summary>
 /// Apply links to the given resource
 /// </summary>
 /// <param name="definitions">Link definitions for the resource</param>
 /// <param name="resource">Resource</param>
 /// <returns>Awaitable task</returns>
 public async Task ApplyAsync(ICollection <LinkDefinition> definitions, ILinkResource resource)
 {
     foreach (LinkDefinition definition in definitions)
     {
         resource.Links[definition.Rel] = new Link
         {
             Rel  = definition.Rel,
             Href = await this.hrefGenerator.GenerateAsync(resource, definition)
         };
     }
 }
Example #3
0
        public void PopulateLinksOnBasicVerbs(ILinkResource linkResource, UrlHelper urlHelper, string controlerName, Guid id)
        {
            if (linkResource == null || urlHelper == null)
            {
                return;
            }
            if (linkResource.Links == null)
            {
                linkResource.Links = new System.Collections.Generic.List <Link>();
            }

            linkResource.Links.Add(
                new Link()
            {
                Rel    = "self",
                Action = "GET",
                Href   = urlHelper.Route("DefaultApi", new { controller = controlerName, id = Guid.NewGuid() }),
                Type   = "\"text/xml\",\"application/json\""
            });
            linkResource.Links.Add(
                new Link()
            {
                Rel    = "self",
                Action = "PUT",
                Href   = urlHelper.Route("DefaultApi", new { controller = controlerName, id = Guid.NewGuid() }),
                Type   = "application/x-www-form-urlencoded"
            });

            linkResource.Links.Add(
                new Link()
            {
                Rel    = "self",
                Action = "DELETE",
                Href   = urlHelper.Route("DefaultApi", new { controller = controlerName, id = Guid.NewGuid() }),
                Type   = ""
            });

            linkResource.Links.Add(
                new Link()
            {
                Rel    = "self",
                Action = "GET",
                Href   = urlHelper.Route("DefaultApi", new { controller = controlerName, id = Guid.NewGuid() }),
                Type   = "\"text/xml\",\"application/json\""
            });
        }
Example #4
0
        public LinkGeneratorTests()
        {
            this.definitions = new[]
            {
                new LinkDefinition {
                    Rel = "self", RouteName = "SelfRoute"
                },
                new LinkDefinition {
                    Rel = "all", RouteName = "AllRoute"
                }
            };

            this.resource = new TestResource();

            this.mockHrefGenerator = new Mock <IHrefGenerator>();
            this.mockHrefGenerator.Setup(g => g.GenerateAsync(resource, this.definitions[0])).ReturnsAsync("/api/test/1");
            this.mockHrefGenerator.Setup(g => g.GenerateAsync(resource, this.definitions[1])).ReturnsAsync("/api/tests");

            this.generator = new LinkGenerator(this.mockHrefGenerator.Object);
        }
Example #5
0
        /// <summary>
        /// Applies links to the given resource
        /// </summary>
        /// <param name="resource">Resource</param>
        /// <returns>Awaitable task</returns>
        private async Task ApplyLinksAsync(ILinkResource resource)
        {
            ICollection <LinkDefinition> definitions = this.repository.Get(resource.GetType());

            await generator.ApplyAsync(definitions, resource);
        }