ServiceLocator.Current.GetInstance<UrlResolver>().GetUrl({Content reference to image})
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.
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.
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,
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); } }
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);
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
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#.