How change “MediaLinkPrefix” Programmatically in sitecore?
-
31-05-2021 - |
Frage
I know "MediaLinkPrefix" can be changed in web.config. But is it possible to change "MediaLinkPrefix" Programmatically? Thanks!
Lösung
To answer your specific question. No. You cannot programmatically change the value of that setting.
If you are trying to use Sitecore.Resources.Media.MediaManager.GetMediaUrl(mediaItem)
to get the URL of a MediaItem
then that is leveraging the Sitecore.Resources.Media.MediaProvider
, which pulls the value of the MediaLinkPrefix
from the Sitecore.Resources.Media.MediaConfig
class. That property does not have a 'setter'. Furthermore, the value of that property actually comes from Sitecore.Configuration.Settings.Media.MediaLinkPrefix
, which also doesn't have a setter and is pulled directly from the config file.
The MediaManager
is a static class that I believe isn't able to be swapped out with a custom implementation. And, unlike the LinkManager
, the MediaManager
is hard coded to use the Sitecore.Resources.Media.MediaProvider
so you can't write a custom provider either.
Andere Tipps
You can do this by creating your own implementation of Sitecore.Links.LinkProvider
and set it up in your web.config under the <linkManager>
section.
I don't know the exact implementation, but you can inherit a class from Sitecore.Links.LinkProvider
and then override the GetItemUrl() method.
Then configure your class to be used as LinkManager.