Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Darren Hunter 140 posts 232 karma points
    Sep 19, 2023 @ 13:28
    Darren Hunter
    0

    I am trying to use "UmbracoHelper" in public control in Umbraco and it generating errors back office it ok.

    I am in the process of writing a sitemap control, and I have hit an issue I am trying to use UmbracoHelper, by injecting it in to a customer control. Without UmbracoHelper in the control constructor the control works, but when I add it to the constructor it falls over.

    I am using the same code as in the back office and there it works, but on a publicly facing control all I seem to get are errors.

    Here the code:

    * * Public Sitemap contoler. */ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.AspNetCore.Mvc.Routing; using Umbraco.Cms.Core.Models.PublishedContent; using Umbraco.Cms.Web.Common; using Microsoft.AspNetCore.Hosting; using System.Text; using emeraldshardlib; using emeraldseositemap.Libs; using Microsoft.Extensions.Logging; using Umbraco.Cms.Core.Services; using System.Collections.Generic; using Org.BouncyCastle.Bcpg.OpenPgp; using System.Linq; using Umbraco.Extensions; using Microsoft.AspNetCore.Authorization; using Polly; using Umbraco.Cms.Web.Common.Authorization; using Umbraco.Cms.Web.Common.Filters;

    namespace emeraldseositemap.Controller.Umbraco {

    public class SiteMapController :  Microsoft.AspNetCore.Mvc.Controller
    {
    
        //Readonly values
        private readonly IUrlHelperFactory _urlHelperFactory;
        private readonly IActionContextAccessor _actionContextAccessor;
    
        private readonly UmbracoHelper _umbracoHelper;
    
        private readonly IWebHostEnvironment _webHostEnvironment;
    
        //New String builder
        private StringBuilder sb = new StringBuilder();
    
        //Shared Function Library
        private SharedFunctions sharedFuncs;
        //Readonly values
        private readonly ILogger<SiteMapController> _logger;
    
        //Doamin service used to get domain!
        private readonly IDomainService _domainService;
    
        //Constructor
        public SiteMapController(
            IUrlHelperFactory urlHelperFactory,
            IDomainService domainService,
            IWebHostEnvironment webHostEnvironment,
            IActionContextAccessor actionContextAccessor,
            ILogger<SiteMapController> logger,
            UmbracoHelper umbracoHelper
            )
        {
    
            _urlHelperFactory = urlHelperFactory;
            _actionContextAccessor = actionContextAccessor;
    
            _umbracoHelper =   umbracoHelper;
            _webHostEnvironment = webHostEnvironment;
            _logger = logger;
            //Build new Shared Function Library. 
            sharedFuncs = new SharedFunctions(_webHostEnvironment, _urlHelperFactory, _actionContextAccessor, _logger);
    
            _domainService = domainService;
    
        }
    
        /*
         * Build the sitemap
         */
        [Route("/Sitemap.xml")]
        public string buildSitemap()
        {
    
            //Build a new object based on sitemap Library. 
            //SiteMapLib map = new SiteMapLib();
            //
            //var node = sharedFuncs.HomeNode(_domainService, _umbracoHelper);
            //
            //IPublishedContent homeNode = sharedFuncs.Home(_umbracoHelper, node);
            //
            //Add the sitemap header
            //sb.AppendLine(map.header());
            //
            //Add site map content. 
            //sb = map.ProcessNode(homeNode,sb);
    
            //Add sitemap footer
            //sb.AppendLine(map.footer());
    
            //Now return the site map.
            //return sb.ToString();
    
            return "testfromControl1";
        }
    }
    

    }

    Any Ideas?

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Sep 20, 2023 @ 09:37
    Marc Goodson
    0

    Hi Darren

    Umbraco determines what is an Umbraco request or what is a front end asset request based on file extension.

    So with the file extension .XML in your route, Umbraco is not adding things like the Umbraco Context to the request cos it thinks it's just a request to a file and so none of it's extra context is required!

    You can tell Umbraco that sitemap.xml and sitemap_index.xml (if you multilingual) are actually requests that should be handled through the Umbraco Pipeline and that you'd very much like the UmbracoContext to exist for them by registering them in UmbracoRequestOptions in Startup.cs in ConfigureServices eg

    services.Configure<UmbracoRequestOptions>(options => options.HandleAsServerSideRequest = httpRequest => httpRequest.Path.StartsWithSegments("/sitemap.xml") || httpRequest.Path.StartsWithSegments("/sitemap_index.xml"));
    

    regards

    Marc

  • Darren Hunter 140 posts 232 karma points
    Sep 20, 2023 @ 09:59
    Darren Hunter
    0

    thank you that worked.

Please Sign in or register to post replies

Write your reply to:

Draft