This code sample has a fault tho: it always returns relative urls, no matter which UrlProviderMode is passed in, so it doesn't work when an Absolute url is requested.
To avoid reimplementing all the logic of the default URL provider, which is the best solution?
The solution I implemented inherits from DefaultUrlProvider and as first thing it asks its Url and then it does some simple string replacement and concatenation to add the yyyy/mm/dd part in the middle of the url.
Here the code that does the trick
public override string GetUrl(UmbracoContext umbracoContext, int id, Uri current, UrlProviderMode mode)
{
var originalContent = base.GetUrl(umbracoContext, id, current, mode);
var content = umbracoContext.ContentCache.GetById(id);
if (content != null && (content.DocumentTypeAlias == "ArticulateRichText" || content.DocumentTypeAlias == "ArticulateMarkdown") && content.Parent != null)
{
var date = content.GetPropertyValue<DateTime>("publishedDate");
if (date != null)
{
var urlFolder = String.Format("{0}/{1:d2}/{2:d2}", date.Year, date.Month, date.Day);
originalContent = originalContent.Replace("/" + content.UrlName, "/" + urlFolder + "/" + content.UrlName);
return originalContent;
}
}
return null;
}
It works, but not sure it's be way. But since I also need to generate absolute urls, I wanted to avoid implementing all the possible cases.
If an absolute URL is request can't you just strip it and turn it into a relative URL?
The examples from the 24 days blog are a bit outdated. For the 1-1 multilingual example I've also created a UrlProvider and ContentFinder. Maybe those are better examples to use. They use the DomainService.
Best way to add yyy/mm/dd to a url via UrlProvider
In order to add a
yyyy/mm/dd
in the middle of a url we need to implement a custom Url provider.There a nice sample online Modify Umbraco URLs with the UrlProvider and ContentFinder.
This code sample has a fault tho: it always returns relative urls, no matter which
UrlProviderMode
is passed in, so it doesn't work when an Absolute url is requested.To avoid reimplementing all the logic of the default URL provider, which is the best solution?
The solution I implemented inherits from
DefaultUrlProvider
and as first thing it asks its Url and then it does some simple string replacement and concatenation to add theyyyy/mm/dd
part in the middle of the url.Here the code that does the trick
It works, but not sure it's be way. But since I also need to generate absolute urls, I wanted to avoid implementing all the possible cases.
Hi Simone,
If an absolute URL is request can't you just strip it and turn it into a relative URL?
The examples from the 24 days blog are a bit outdated. For the 1-1 multilingual example I've also created a UrlProvider and ContentFinder. Maybe those are better examples to use. They use the DomainService.
https://github.com/jbreuer/1-1-multilingual-example/blob/master/Sources/Umbraco.Extensions/UrlProviders/MultilingualUrlProvider.cs
https://github.com/jbreuer/1-1-multilingual-example/blob/master/Sources/Umbraco.Extensions/ContentFinders/MultilingualContentFinder.cs
Jeroen
I'm afraid I cannot.
If the
Content.UrlAbsolute
is called, the caller expects an absolute URL.For example when rendering the metadata for open graph or in RSS feeds or for Disqus integration
Both samples don't return it.
is working on a reply...