I am trying to get an image from Umbraco in my own class.
I can´t understand why I can not use @umbraco helper in my class. I have its namespace defined in the class, such as:
using Umbraco.Core.Models;
using Umbraco.Web;
I can only use UmbracoHelper, when I write:
var umbracoHelper = new UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
Here is my class:
using Umbraco.Core.Models;
using Umbraco.Web;
namespace House.Site.Helpers
{
public static class ImageClass
{
public static string GetAbsoluteImageUrl(this IPublishedContent node, string propertyName)
{
var umbracoHelper = new UmbracoHelper(Umbraco.Web.UmbracoContext.Current);
var imgID = node.GetPropertyValue<string>("propertyName");
var imgObject = umbracoHelper.TypedMedia(imgID);
return imgObject.Url;
}
}
}
Since your class is not inheriting from any Umbraco base classes you will have to instantiate the UmbracoHelper yourself.
Doing so by using:
var umbracoHelper = new UmbracoHelper(UmbracoContext.Current)
is perfectly fine and will get you access to what you need.
You should be able to use just UmbracoContext.Current
instead of the full namespace and class name, since you already have the using
statement for Umbraco.Web
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With