Monday, 12 August 2013

Umbraco get media id from url

Working with umbraco 6 is great fun.

I am working with images and needed to access image before is served to public.
Hope my solution will  save you some time.

Here is solution

var path = HttpContext.Current.Request.Path.ToLower();

// filter requests that we know are not relevant
if (!path.StartsWith("/media/") || path.StartsWith("/media/temp/") || path.Contains("_thumb.")) return;

// access medisa service
Umbraco.Core.Services.MediaService ms = new Umbraco.Core.Services.MediaService(new Umbraco.Core.Persistence.RepositoryFactory()); 
var item = ms.GetMediaByPath(path);

No comments:

Post a Comment