Relative Path in Web Part resolving to path of ascx instead of node url

Sam Loesch asked on February 27, 2017 06:34

I have a web part setup with a UrlSelector field that's set with a relative path.

LinkCard.Attributes.Add("href", NavigationUrl); //NavigationUrl = "./Press-Releases"

When the webpart renders, the relative path gets appended to the path of the webpart control.

Image Text

I would expect this relative path to be applied to the NodeAliasPath of the current document, as it seems to do in other webparts. Am I missing something here?

EDIT: I've tried ResolveUrl(NavigationUrl) but it just gives me "/CMSWebParts/Press-Releases"

Recent Answers


Mariia Hrytsai answered on February 27, 2017 10:36

I think both two methods resolve the path using the current folder path where the web part is. If you want to have the current page path were the web part is placed you should try something like this NavigationUrl = CMS.DocumentEngine.DocumentContext.CurrentDocument.NodeAliasPath + "/Press-Releases". It will resolve the path correctly.

2 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on February 27, 2017 15:50

I use this without issue in any webpart I build:

sectHero.Attributes.Add("style", string.Format("background: url('{0}')", CMS.Helpers.URLHelper.ResolveUrl(SectionImage)));

0 votesVote for this answer Mark as a Correct answer

Sam Loesch answered on February 27, 2017 16:35

Mariia, it seems odd that it's up to me to parse "./" when it's a supported Kentico path expression. there's no api method for resolving these supported expressions? https://docs.kentico.com/k8/developing-websites/loading-and-displaying-data-on-websites/loading-document-data/writing-document-path-expressions

Brenden, I don't think SectionImage would be using a relative path, so I'm unclear how this is related.

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on February 27, 2017 22:07

Sam,

The property SectionImage IS a relative path coming from one of two places, a media library file or a content page using the URL Selector. If you attempt to let the webpart resolve a URL it will resolve it relative to the path the webpart is in. You want it to be resolved based on the page the webpart is placed on, hence using the URLHelper.ResolveUrl()

1 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.