The code is correct, but Node.GetCurrent is legacy. Beside Umbraco.TypedMedia you also have Umbraco.TypedContent. With that you also get the property. Something like this:
UmbracoHelper umbracoHelper = new UmbracoHelper(Umbraco.Web.UmbracoContext.Current); Response.Redirect(umbracoHelper.TypedMedia(umbracoHelper.TypedContent(id).GetPropertyValue<int>("mediaFile")).UrlName);
You can also get the current node through the Umbraco helper like this:
UmbracoHelper umbracoHelper = new UmbracoHelper(Umbraco.Web.UmbracoContext.Current); Response.Redirect(umbracoHelper.TypedMedia(umbracoHelper.AssignedContentItem.GetPropertyValue<int>("mediaFile")).UrlName);
Sorry I forgot to mention that .GetPropertyValue<> is an extension method. So you need to add the Umbraco.Core namespace before you can use it.
If you want to full url try something like this:
var uri = new Uri(HttpContext.Current.Request.Url.AbsoluteUri);
var domainUrl = string.Format("{0}://{1}", uri.Scheme, uri.Authority); Response.Write(domainUrl + umbracoHelper.TypedMedia(umbracoHelper.AssignedContentItem.GetPropertyValue<int>("mediaFile").Value).UrlName)
Get Media ID
Is there any better way to do this? I want to display a media item directly in the browser. Parsing the property seems kind of convoluted.
Thanks,
-Jim
Hi what version of Umbraco are you using? Charlie
Hello,
If you are using Umbraco 6 or higher you can use Umbraco.TypedMedia(id) for this: http://our.umbraco.org/documentation/Reference/Mvc/querying
Jeroen
@Charles Afford, I'm using 4.11.10.
Thanks Jeroen Breuer ... any suggestions for 4.x?
-Jim
Uhm I think the feature was actually added in Umbraco 4.10 already so you can probably use it.
Jeroen
Yea i think TypedMedia was added as well.
So you can do (i think)
@Umbraco.TypedMedia(Model.Content.GetPropertyValue("your alias")).Url
I'm using Web forms so it looks like I would expose the UmbracoHelper and then use TypedMedia. Does this sound right?
UmbracoHelper umbracoHelper = new UmbracoHelper(Umbraco.Web.UmbracoContext.Current); Response.Redirect(umbracoHelper.TypedMedia(Node.GetCurrent().GetProperty("mediaFile")).UrlName);
Hello,
The code is correct, but Node.GetCurrent is legacy. Beside Umbraco.TypedMedia you also have Umbraco.TypedContent. With that you also get the property. Something like this:
You can also get the current node through the Umbraco helper like this:
Jeroen
Jeron,
wouldn't compile because IPublishedContent doesn't have a GetPropertyValue member so I changed it to:
Response.Write(umbracoHelper.TypedMedia(umbracoHelper.AssignedContentItem.GetProperty("mediaFile").Value).UrlName)but that only gives me the name of the media...I need the full URL. How would you recommend I obtain that?
Thanks,
-Jim
Hello,
Sorry I forgot to mention that .GetPropertyValue<> is an extension method. So you need to add the Umbraco.Core namespace before you can use it.
If you want to full url try something like this:
var uri = new Uri(HttpContext.Current.Request.Url.AbsoluteUri); var domainUrl = string.Format("{0}://{1}", uri.Scheme, uri.Authority);Response.Write(domainUrl + umbracoHelper.TypedMedia(umbracoHelper.AssignedContentItem.GetPropertyValue<int>("mediaFile").Value).UrlName)
Jeroen
That won't give me the full path. The following seems to work however.
UmbracoHelper umbracoHelper = new UmbracoHelper(Umbraco.Web.UmbracoContext.Current); int iMediaFileId = umbracoHelper.AssignedContentItem.GetPropertyValue<int>("mediaFile"); string sMediaURL = umbracoHelper.TypedMedia(iMediaFileId).GetPropertyValue<string>("umbracoFile"); Response.Redirect(sMediaURL);Yep that will do it Jim. You need to get the value of "umbracoFile"
is working on a reply...
This forum is in read-only mode while we transition to the new forum.
You can continue this topic on the new forum by tapping the "Continue discussion" link below.