2018年3月12日

(Sitecore) - To obtain URL of media item in Sitecore

Typically it is not too difficult to put image in your Sitecore page through image field of template, but there is no way directly obtain image URL through the Admin platform, this article address how to obtain URL of image in order to use for CSS or somewhere you need.



This work can be splitted into two part, first part is Sitecore and then is .Net MVC.

Sitecore Part

Fist, you have to upload your images to Editor platform under the Media Library, a suggestion is you can create a specific folder to put relative images in oder to quick search.


There is a field required but I miss cause View empty output. The Alt field must be assigned a value.



Finally this media item can be used in content





.Net MVC Part

Please create a static class named as MediaExtensions (or whatever you want), then create a function call getUrl which require a field name parameter input, put the following code into function.



if you complete above coding, then you can start to use it at View.

@MediaItemExtensions.getUrl("YourFieldName")






Reference : 

https://sitecore.stackexchange.com/questions/1230/how-do-you-get-the-link-for-a-media-item/1251

https://stackoverflow.com/questions/6122901/get-media-url-including-server-part

沒有留言:

張貼留言

<Javascript> How to uncompressed GZIP at front-end using Javascript

It's been a while I haven't share my coding work. In this article I would like to share how to receive a Gzip file via stream, unzip...