Copied to clipboard

Flag this post as spam?

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

  • lucuma 261 posts 563 karma points
    Sep 11, 2014 @ 19:31

    Use ContentService/MediaService inside HttpModule

    Umbraco v7.x

    Can anyone explain how to access or instantiate the Umbraco core services inside an http module. I can't seem to get it working.

     public void OnBeginRequest(Object sender, EventArgs e)
         var httpApp = (Umbraco.Web.UmbracoApplication)sender;
         // how the heck do i get to the services?
         var myMedia = Umbraco.Core.ApplicationContext.Current.Services.MediaService.GetById(1000);
        // myMedia is always null

    I'm trying to intercept the requests before some other modules (ie the imageprocessor).


  • Dan Lister 416 posts 1973 karma points c-trib
    Sep 12, 2014 @ 09:36
    Dan Lister


    You could always do the following, although it doesn't use any of the services.

    var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
    var media = umbracoHelper.TypedMedia(1000);

    Thanks, Dan.

  • lucuma 261 posts 563 karma points
    Sep 12, 2014 @ 13:40

    UmbracoContext.Current is null so it won't work.

  • Tom van Enckevort 104 posts 416 karma points
    Sep 12, 2014 @ 13:57
    Tom van Enckevort

    Are you sure you have a media item with an ID of 1000?

    Because the fact that you can make the call without any NullReferenceExceptions kind of proves that the service has been instantiated alright.

  • lucuma 261 posts 563 karma points
    Sep 12, 2014 @ 14:06

    That was actually a placeholder for this post, but shoot you might be right. I was thinking that the URL /media/1001/file.png had the id in it but it looks like that is a wrong assumption..... So now I need to see how I can map the file name to a media item....

Please Sign in or register to post replies

Write your reply to: