Exemple #1
0
        public ActionResult GetRedirectsForNode(string type, Guid key)
        {
            try {
                RedirectNodeModel node;

                switch (type)
                {
                case "content":

                    // Get a reference to the content item
                    IContent content1 = _contentService.GetById(key);

                    // Trigger an exception if the content item couldn't be found
                    if (content1 == null)
                    {
                        throw new RedirectsException(HttpStatusCode.NotFound, _backOffice.Localize("errorContentNoRedirects"));
                    }

                    // Look up the content via the content cahce
                    IPublishedContent content2 = null;
                    if (_umbracoContextAccessor.TryGetUmbracoContext(out IUmbracoContext umbraco))
                    {
                        content2 = umbraco.Content.GetById(key);
                    }

                    // Initialize a new model instance
                    node = new RedirectNodeModel(content1, content2);

                    break;

                case "media":

                    // Get a reference to the media item
                    IMedia media1 = _mediaService.GetById(key);

                    // Trigger an exception if the media item couldn't be found
                    if (media1 == null)
                    {
                        throw new RedirectsException(HttpStatusCode.NotFound, _backOffice.Localize("errorContentNoRedirects"));
                    }

                    // Look up the media via the content cahce
                    IPublishedContent media2 = null;
                    if (_umbracoContextAccessor.TryGetUmbracoContext(out umbraco))
                    {
                        media2 = umbraco.Content.GetById(key);
                    }

                    // Initialize a new model instance
                    node = new RedirectNodeModel(media1, media2);

                    break;

                default:
                    throw new RedirectsException(HttpStatusCode.BadRequest, $"Unsupported node type: {type}");
                }

                // get the redirects via the redirects service
                IRedirect[] redirects = _redirects.GetRedirectsByNodeKey(node.Type, key);

                // Generate the response
                return(new JsonResult(new {
                    node,
                    redirects = _backOffice.Map(redirects)
                }));
            } catch (RedirectsException ex) {
                // Generate the error response
                return(Error(ex));
            }
        }