Umbraco 中獲取一個media item的文件路徑 file path
阿新 • • 發佈:2018-11-30
pre ica urn setting cat set app www. prope
我們要使用UmbracoHelper, 這裏就需要用到我們在之前的blog裏面寫的UmbracoContext
參看這個blog https://www.cnblogs.com/wphl-27/p/9927044.html
public class ContextHelpers { public static UmbracoContext EnsureUmbracoContext() {
if(UmbracoContext != null) { return UmbracoContext.Current; }var dummyHttpContext = new HttpContextWrapper(new HttpContext(new SimpleWorkerRequest("blah.aspx", "", new StringWriter()))); return UmbracoContext.EnsureContext( dummyHttpContext, ApplicationContext.Current, new WebSecurity(dummyHttpContext, ApplicationContext.Current), UmbracoConfig.For.UmbracoSettings(), UrlProviderResolver.Current.Providers,false); } }
1. 在View 中
在View中寫如下代碼
UmbracoHelper uHelper = new UmbracoHelper(ContextHelper.EnsureUmbracoContext()); string mediaUrl = ""; if (CurrentPage.HasValue(propertyName)) { var mediaItem = uHelper.Media(CurrentPage.propertyName.ToString()); mediaUrl = mediaItem.umbracoFile; }return mediaUrl;
2. 在後臺代碼中寫
using Umbraco.Web; public static string GetMediaUrlFromPropertyName(dynamic contentItem, string propertyName) { UmbracoHelper uHelper = new UmbracoHelper(ContextHelper.EnsureUmbracoContext()); string mediaUrl = ""; if (contentItem.HasValue(propertyName)) { var mediaItem = uHelper.Media(contentItem.GetPropertyValue(propertyName)); mediaUrl = mediaItem.umbracoFile; } return mediaUrl; }
原文請參看 https://codeshare.co.uk/blog/how-to-get-the-file-path-of-a-media-item-in-umbraco/
Umbraco 中獲取一個media item的文件路徑 file path