升级后无法从Umbraco获取媒体图像URL

时间:2017-08-01 14:33:11

标签: umbraco upgrade media

我最近升级到7.6.3,我在查看视图中检索媒体图像网址时出现问题。为了测试purpposes,我为其中一个节点添加了一个新的MediaPicker2属性,为它设置了一个值并尝试在我的剃刀视图中获取它的值:

var icon2 = Model.Content.GetProperty("icon2");

该对象看起来像这样: enter image description here

执行Model.Content.GetPropertyValue("icon2")会引发以下错误:

  

Umbraco.Core.dll中出现“System.Exception”类型的异常,但未在用户代码中处理

     

其他信息:创建值时出现异常。

     

InnerException:“对象引用未设置为对象的实例。”

Model.Content.GetPropertyValue<IPublishedContent>("icon2")会抛出相同的错误。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

根据this answer,你所做的是正确的。但是,我不确定为什么它不起作用。

但是,我之前已经回答了类似的问题,I found a roundabout way of getting the node for a Udi

您可以使用以下代码从图像字符串中获取IPublishedContent:

// Your string.
var imageString = Model.Content.GetPropertyValue<string>("icon2");

// Get the guid from this string.
var imageGuidUdi = GuidUdi.Parse(imageString);

// Get the ID of the node!
var imageNodeId = ApplicationContext.Current.Services.EntityService.GetIdForKey(guidUdi.Guid, (UmbracoObjectTypes)Enum.Parse(typeof(UmbracoObjectTypes), guidUdi.EntityType, true));

// Finally, get the node.
var imageNode = Umbraco.TypedMedia(imageNodeId.Result);
相关问题