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";
}
}
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
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 {
}
Any Ideas?
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
regards
Marc
thank you that worked.
is working on a reply...