开发者

Calling node.NiceUrl gives me # in Umbraco

Doing a project in Umbraco, and i've encountered problems in one case that when calling node.NiceUrl I get # as the result. What is weird though is that if i debug it somehow it resolves into the correct url.

var pages = Pa开发者_运维问答ges.Select((item, index) => new
{
    Url = item.NiceUrl,
    Selected = item.Id == currentPage.Id,
    Index = index
}).ToList();

Where Pages is obtained from:

CurrentPage.Parent.ChildrenAsList


If I do it this way, it works, but I don't know why.

 Url = new Node(item.Id).NiceUrl,


I've encountered this error and it was because the id belonged to a media node.

Media is treated differently to other content and there's no easy way of getting the url because different types of media store the url in different ways depending on context. That's why the NiceUrl function doesn't work for media (according to the umbraco developers).

My specific scenario was using images that had been selected using a media picker. I got the url via the following code. I wrapped it up in an extension method so you can consume it from a template in a convenient way.

public static string GetMediaPropertyUrl(this IPublishedContent thisContent, string alias, UmbracoHelper umbracoHelper = null)
{
    string url = "";

    if (umbracoHelper == null)
        umbracoHelper = new UmbracoHelper(UmbracoContext.Current);

    var property = thisContent.GetProperty(alias);

    string nodeID = property != null ? property.Value.ToString() : "";

    if (!string.IsNullOrWhiteSpace(nodeID))
    {
        //get the media via the umbraco helper
        var media = umbracoHelper.TypedMedia(nodeID);

        //if we got the media, return the url property
        if (media != null)
            url = media.Url;
    }

    return url;
}


Try like this

    Url = umbraco.library.NiceUrl(Item.Id);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜