getting a page url mvc .net core

lawrence whittemore asked on April 28, 2022 16:42

Is there a way that is considered the better way to get a page URL. (both of these are in my view models). I noticed just using something like this (url = test.RelativeURL) says it is deprecated

using documentUrlProvider

 PageUrl = DocumentURLProvider.GetUrl(announcement)

or using pageUrlRetriever

Url = pageUrlRetriever.Retrieve(menuItem).RelativePath,

Both ways work fine for me, just curious if one was best practice

Recent Answers


Sean Wright answered on June 9, 2022 06:28

Always use types that have an interface (eg IPageRetriever vs DocumentHelper or IPageUrlRetriever vs DocumentURLProvider.

Why?

  1. Because it will result in more testable code.
  2. The Kentico Xperience dev team has been migrating away from these static classes and is building functionality for Kentico Xperience 13 into the interface abstractions they are providing.

So, DocumentURLProvider might work today, but it's not going to cover the edge cases like IPageUrlRetriever will (it's not going to be 'Content Tree routing' aware), and it's not guaranteed to give you the expected result when you eventually upgrade your site to the next version.

0 votesVote for this answer Mark as a Correct answer

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