Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • claudio morandi 24 posts 92 karma points
    3 days ago
    claudio morandi
    0

    How to get a media by url without hitting the database?

    Hello... I need to correct a middleware that checks a media property for each request. It's currenlty working but it tells me that the method I use is obsolete and will be removed on next version (14) of Umbraco.

    I should obtain a media by its url but without hitting the db. Can you help me?

    I currently use this code

    IPublishedMediaCache? mediaCache = umbracoContextReference.UmbracoContext.Media;

    var foundMedia = mediaCache.GetSingleByXPath($"//umbracoFile[contains(., '{context.Request.Path}')]");

    Thank you so much

  • Sam Marshallsay 37 posts 137 karma points
    1 day ago
    Sam Marshallsay
    0

    You could probably just get all descendants with umbracohelper and filter them with LINQ. Something like:

    var matchingMedia = _uh.MediaAtRoot().SelectMany(x => x.DescendantsOrSelf()).FirstOrDefault(x => x.Value<string>("umbracoFile").Contains("request-path-here"));
    

    And tweak it a bit to ignore case etc

Please Sign in or register to post replies

Write your reply to:

Draft