get image url from library

Vote:
 

i have block with image property.

when am selecting image am getting image id.

but when i want to display image in cshtml i need image src. so using image id how can i get image src value.

i want in c#.

#174351
Edited, Jan 25, 2017 7:19
Vote:
 

ServiceLocator.Current.GetInstance<UrlResolver>().GetUrl({Content reference to image})

#174353
Jan 25, 2017 7:54
Vote:
 

 string url;

if (currentBlock.BlockContentArea != null && currentBlock.BlockContentArea.Items.Any())
{
foreach (var contentItem in currentBlock.BlockContentArea.Items)
{
var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();
var contentOfBlock = contentLoader.Get<IContentData>(contentItem.ContentLink);

url = UrlResolver.Current.GetUrl(contentOfBlock.Property["image"].ToString());

}
}

here am getting image id in contentOfBlock.Property["image"].ToString() as 87.

so to view i need to pass img src. so using image id how can i get image src value.

#174361
Edited, Jan 25, 2017 8:08
Vote:
 

If you are accessing "image" property - I would recommend installing of asking for IContentData, better ask for type of your block (or some interface where Image property is defined) which is implemented by all blocks added to content area. If Image property is of type ContentReference UrlResolver should give back public URL.

#174365
Jan 25, 2017 8:30
Vote:
 

here is my image property details.

[Display(
GroupName = SystemTabNames.Content,
Order = 1
)]
[CultureSpecific]
[UIHint(UIHint.Image)]
public virtual ContentReference Image { get; set; }

how can i get image src value,

#174369
Jan 25, 2017 8:48
Vote:
 

Try

string url;
var contentLoader = ServiceLocator.Current.GetInstance<IContentLoader>();

if (currentBlock.BlockContentArea != null && currentBlock.BlockContentArea.Items.Any())
{
    foreach (var contentItem in currentBlock.BlockContentArea.Items)
    {
        var contentOfBlock = contentLoader.Get<IContentData>(contentItem.ContentLink);

        if (contentOfBlock["Image"] == null)
        {
            continue;
        }

        url = UrlResolver.Current.GetUrl(contentOfBlock["Image"] as ContentReference);
    }
}
#174371
Jan 25, 2017 9:37
Vote:
 

Can you ask type of your block?

contentLoader.Get<MyBlock>(contentItem.ContentLink);

And then you don't need to access property via stringly typed interface:

url = UrlResolver.Current.GetUrl(contentOfBlock.Image);
#174376
Jan 25, 2017 10:39
Vote:
 

Hi sandeep,
I think the below code is usefull to you to get the image Url.


@Url.ContentUrl(Model.Image)


try this code on the View page (.cshtml),
 like, <span>@Url.ContentUrl(Model.Image)</span>


Now, you can see the Url of the Image while Inspect the Output Site Page.

Try and let me know

Thank You

#185566
Nov 24, 2017 10:18
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.