@Umbraco.TypedMedia(MyProperty) 在 Umbraco 7.6.1 中为 null [英] @Umbraco.TypedMedia(MyProperty) is null in Umbraco 7.6.1

查看:23
本文介绍了@Umbraco.TypedMedia(MyProperty) 在 Umbraco 7.6.1 中为 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是 Umbraco 7.6.1,我正在尝试使用下面的代码在 razor 中获取媒体图像的 url.

I'm using Umbraco 7.6.1 and I'm trying to get the url for a media image using the code below in razor.

@{
   var bannerMediaItem = Umbraco.TypedMedia(Model.Content.Image); 
   var bannerUrl = bannerMediaItem.Url;
}

但即使我知道它存在,bannerMediaItem 始终为空.如果我这样做..

But bannerMediaItem is always null even though I know it exists. If I do this..

@Model.Content.Image

它返回图像的 id,例如1086

It returns the id of the image e.g. 1086

推荐答案

在 Umbraco v7.6 及更高版本中,现在有一个名为 UDI 的新事物.在此处了解有关 UDI 的更多信息.https://our.umbraco.org/documentation/reference/querying/Udi

In Umbraco v7.6 and above, there is now a new thing called UDI. Find out more about UDI here. https://our.umbraco.org/documentation/reference/querying/Udi

因此,默认情况下,您的媒体项目不是具有数字 ID,而是具有如下 ID:umb://media/39d3ac707d634953ab52642d5037855c

So instead of your media item having a numeric id, by default it will have an id like this: umb://media/39d3ac707d634953ab52642d5037855c

以下是当 id 是新类型之一时获取媒体 url 的方法:

Here is how you get the media url when the id is one of the new type:

string imageUrl = Model.Content.GetPropertyValue<IPublishedContent>("headerImage").Url;

这篇关于@Umbraco.TypedMedia(MyProperty) 在 Umbraco 7.6.1 中为 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆