Exemplo n.º 1
0
        public override Task GetContentItemAspectAsync(ContentItemAspectContext context)
        {
            return(context.ForAsync <ContentItemMetadata>(async metadata =>
            {
                // When a content item is contained we provide different route values when generating urls.
                (var found, var entry) = await _autorouteEntries.TryGetEntryByContentItemIdAsync(context.ContentItem.ContentItemId);

                if (found && !string.IsNullOrEmpty(entry.ContainedContentItemId))
                {
                    metadata.DisplayRouteValues = new RouteValueDictionary {
                        { "Area", "OrchardCore.Contents" },
                        { "Controller", "Item" },
                        { "Action", "Display" },
                        { "ContentItemId", entry.ContentItemId },
                        { "ContainedContentItemId", entry.ContainedContentItemId }
                    };
                }
            }));
        }
        public IEnumerable <Endpoint> FindEndpoints(RouteValuesAddress address)
        {
            if (address.AmbientValues == null || address.ExplicitValues == null)
            {
                return(Enumerable.Empty <Endpoint>());
            }


            // Try to get the contained item first, then the container content item
            string contentItemId = address.ExplicitValues[_options.ContainedContentItemIdKey]?.ToString();

            if (string.IsNullOrEmpty(contentItemId))
            {
                contentItemId = address.ExplicitValues[_options.ContentItemIdKey]?.ToString();
            }

            if (string.IsNullOrEmpty(contentItemId))
            {
                return(Enumerable.Empty <Endpoint>());
            }

            (var found, var autorouteEntry) = _entries.TryGetEntryByContentItemIdAsync(contentItemId).GetAwaiter().GetResult();

            if (!found)
            {
                return(Enumerable.Empty <Endpoint>());
            }

            if (Match(address.ExplicitValues))
            {
                // Once we have the contained content item id value we no longer want it in the route values.
                address.ExplicitValues.Remove(_options.ContainedContentItemIdKey);

                var routeValues = new RouteValueDictionary(address.ExplicitValues);

                if (address.ExplicitValues.Count > _options.GlobalRouteValues.Count + 1)
                {
                    foreach (var entry in address.ExplicitValues)
                    {
                        if (String.Equals(entry.Key, _options.ContentItemIdKey, StringComparison.OrdinalIgnoreCase))
                        {
                            continue;
                        }

                        if (!_options.GlobalRouteValues.ContainsKey(entry.Key))
                        {
                            routeValues.Remove(entry.Key);
                        }
                    }
                }

                var endpoint = new RouteEndpoint
                               (
                    c => null,
                    RoutePatternFactory.Parse(autorouteEntry.Path, routeValues, null),
                    0,
                    null,
                    null
                               );

                return(new[] { endpoint });
            }

            return(Enumerable.Empty <Endpoint>());
        }