Try our conversational search powered by Generative AI!

Alt tag for image

Vote:
 

This somes like a really obvious thing but I cannot find any information about it anywhere. I have a property of type Url with the UiHint.Image decoration applied to it. I want to render that image on my page along with the description applied in the file manager as an alt tag. My thoughts where to modify the DisplayTemplate for the image. I have the following code to get the alt tag:

var file = System.Web.Hosting.HostingEnvironment.VirtualPathProvider.GetFile(virtualPath) as UnifiedFile;
var alt = file != null && file.Summary.Dictionary["Description"] != null ? file.Summary.Dictionary["Description"].ToString() : string.Empty;

    

The problem I am having however is the data that I have available for me is just of type EPiServer.Url, from this I need to find out what the virtualpath for that url is.

Does anyone know how I can obtain this, or have a better suggestion on how to do it?

Many Thanks
Dave

#74726
Sep 05, 2013 15:32
Vote:
 

Try using the PermanentLinkMapper class and TryToMapped() method.

Frederik

#74741
Sep 05, 2013 19:14
Vote:
 

Hi Frederik

Thank you for your response!!! Unfortunately the URL that comes back is tho relative mapped path, not the original Virtual Path. Any other thoughts?

Many thanks
Dave

#74796
Sep 06, 2013 17:43
Vote:
 

Hi All

Has anyone got any idea on this? There has to be a way to get an alt tag for an image.

Thanks very much
Dave

#75249
Sep 23, 2013 15:55
Vote:
 

Hi all

For future reference I have got it working, it was as simple as URLDecoding first, I have written an extension method which I am just calling from my DisplayTemplate and a few other places where I am outputting images, works perfectly. Code below if it helps anyone:

public static string GetDescription(this Url url)
        {
            var description = string.Empty;
            var virtualPath = HttpUtility.UrlDecode(url.ToString());
            var file = VirtualPathHandler.Instance.GetFile(virtualPath, true) as UnifiedFile;

            if (file != null)
            {
                description = file.Summary.Dictionary["Description"] != null
                                  ? file.Summary.Dictionary["Description"].ToString()
                                  : string.Empty;
            }

            return description;
        }

    

Thanks
Dave

#75820
Oct 07, 2013 17:42
This thread is locked and should be used for reference only. Please use the Episerver CMS 7 and earlier versions forum to open new discussions.
* 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.