获取媒体URL鉴于Sitecore的MVC [英] Get media url in View in Sitecore MVC
问题描述
我有一个是存储在媒体库中的mp3文件。它是在一个媒体字段引用上的项目。我可以在web表单或类做Sitecore.Resources.Media.MediaManager.GetMediaURL()获取URL。但是,我怎么能在一个MVC视图实现这一目标?当我倾倒场,它只是呈现一个HTML标签。我只想拔出网址。
I have a mp3 file that is stored in the media library. It is referenced in a media field on an item. I can get the URL by doing Sitecore.Resources.Media.MediaManager.GetMediaURL() in webforms or a class. But how can I achieve this in a MVC view? When I dump the field it just renders a html tag. I want to pull out the url only.
此外,有没有这样的事情的参考?
Also, is there a reference for something like this?
感谢
推荐答案
您可以编写扩展方法来获得媒体项目URL。像这样的东西就可以了(你应该添加空引用检查BTW):
You can write an extension method to get the media item URL. Something like this will work (you should add null reference checks btw):
public static string GetMediaUrl(this Sitecore.Mvc.Helpers.SitecoreHelper sitecoreHelper, string fieldName)
{
return GetMediaUrl(sitecoreHelper, fieldName, sitecoreHelper.CurrentItem);
}
public static string GetMediaUrl(this Sitecore.Mvc.Helpers.SitecoreHelper sitecoreHelper, string fieldName, Item item)
{
MediaItem mediaItem = item.Fields[fieldName].Item;
return Sitecore.Resources.Media.MediaManager.GetMediaUrl(mediaItem);
}
在您的CSHTML文件,然后你可以使用:
In your cshtml files you can then use:
<a href="@Html.Sitecore().GetMediaUrl("YourMediaField")">Click Here</a>
这篇关于获取媒体URL鉴于Sitecore的MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!