I'm trying to convert a string(URL) to a normal Umbraco Media item/ID.
I have no idea if this the smartest way to do it but this is how far I'm right now:
var imageUrl = "/media/1103/test-product.png";
if (imageUrl.StartsWith("/media/"))
{
String imageUrlString = imageUrl;
String[] words = imageUrlString.Split('/');
var imageId = 0;
foreach (string word in words.Skip(2).Take(1))
{
imageId = Int32.Parse(word);
}
if(imageId > 0)
{
//Check ID
<h1>@imageId</h1>
// convert to Umbraco Media
var image = Umbraco.Media(imageId);
<h1>Test: @image.Name</h1>
}
}
}
So my problem is to convert it to an media it so I can call @image.GetCropUrl() @image.Name etc.
The folder name "1103" is not the media item id which is why it doesn't work.
There is one method to do this on the MediaService but as with all services this shouldn't be utilised on the front end of the website as management services are not designed for this purpose and therefore not cached.
However if you have no other option then it is done like this
var imageUrl = "/media/1103/test-product.png";
var ms = ApplicationContext.Services.MediaService;
var mediaId = ms.GetMediaByPath(imageUrl).Id;
You should implement some caching if you were to use this on the front end of the website.
var ms = ApplicationContext.Services.MediaService;
var mediaId = ms.GetMediaByPath(imageUrl).Id;
shouldn't be used to render a page. The services will make database queries, they are not designed to be used to render pages but for management operations in the back end of Umbraco.
If you absolutely have to use them, then you should ensure that you are somehow caching the result so that the database queries aren't executing on every request. In it's simplest form you could cache the result in a cached partial view. Another approach might be to use Output caching such as MVCDonutCaching or to utilise RuntimeCache (you would need an event to clear this on media save also).
Is there absolutely no way that uCommerce can give you a id/udi rather than only a url? I would recommend you explore this before committing to using the media service.
Get Media Id from URL
Hi guys
I'm trying to convert a string(URL) to a normal Umbraco Media item/ID.
I have no idea if this the smartest way to do it but this is how far I'm right now:
So my problem is to convert it to an media it so I can call @image.GetCropUrl() @image.Name etc.
The ImageURL is generated from the uCommerce API.
The folder name "1103" is not the media item id which is why it doesn't work.
There is one method to do this on the MediaService but as with all services this shouldn't be utilised on the front end of the website as management services are not designed for this purpose and therefore not cached.
However if you have no other option then it is done like this
You should implement some caching if you were to use this on the front end of the website.
Jeavon
Hi Jeavon
That's perfect. Well, and a bit stupid of me :D
The imageURL is a media picker from uCommerce so i'm not woried about the caching. Or am I missing something? :)
The issue is that this
shouldn't be used to render a page. The services will make database queries, they are not designed to be used to render pages but for management operations in the back end of Umbraco.
If you absolutely have to use them, then you should ensure that you are somehow caching the result so that the database queries aren't executing on every request. In it's simplest form you could cache the result in a cached partial view. Another approach might be to use Output caching such as MVCDonutCaching or to utilise RuntimeCache (you would need an event to clear this on media save also).
Is there absolutely no way that uCommerce can give you a id/udi rather than only a url? I would recommend you explore this before committing to using the media service.
Ohh, that sucks :/
Right now i can get the image like this:
But that only returns the url.
I've contacted the uCommerce Team.
Is this also a direct call to the database?
I don't know I'm afraid, that's a uCommerce service.
It would be much better if you could have
Model.ProductViewModel.ThumbnailImageId
orModel.ProductViewModel.ThumbnailImage.Id
Hopefully they can get you a id/udi
Agree :D
Thanks Jeavon :D
is working on a reply...