0

I have a special folder in the Sitecore media library (v9.1.1) that I would like to run a custom MediaRequestHandler on. The path to the folder is /media library/files/securefiles.

I read this post (https://sitecoreblog.patelyogesh.in/2013/09/sitecore-custom-http-handlers.html) that made it seem like I could create a custom handler and use the Trigger to specify the prefix for the handler. But it doesn't seem to be working. Here is what I currently have:

In web.config

<configuration>
  <system.webServer>
    <handlers>
      <add verb="*" path="sitecore_media.ashx" type="Sitecore.Resources.Media.MediaRequestHandler, Sitecore.Kernel" name="Sitecore.MediaRequestHandler" />
      <add verb="*" path="sitecore_media_secure1.ashx" type="MyCustomMediaRequestHandler, MyCustomAssembly" name="MyCustomMediaRequestHandler" />
    </handlers>
  </system.webServer>
</configuration>

And in Sitecore.Config

<sitecore>
  <mediaLibrary>
    <mediaPrefixes>
      <prefix value="-/media/files/securefiles" />
    </mediaPrefixes>
  </mediaLibrary>

  <customHandlers>
    <handler trigger="-/media/" handler="sitecore_media.ashx" />
    <handler trigger="~/media/" handler="sitecore_media.ashx" />
    <handler trigger="~/api/" handler="sitecore_api.ashx" />
    <handler trigger="-/api/" handler="sitecore_api.ashx" />
    <handler trigger="-/xaml/" handler="sitecore_xaml.ashx" />
    <handler trigger="~/xaml/" handler="sitecore_xaml.ashx" />
    <handler trigger="-/icon/" handler="sitecore_icon.ashx" />
    <handler trigger="~/icon/" handler="sitecore_icon.ashx" />
    <handler trigger="-/temp/" handler="sitecore_temp.ashx" />
    <handler trigger="~/temp/" handler="sitecore_temp.ashx" />
    <handler trigger="~/feed/" handler="sitecore_feed.ashx" />
    <handler trigger="-/feed/" handler="sitecore_feed.ashx" />
    <handler trigger="-/media/files/securefiles/" handler="sitecore_media_secure1.ashx" />
    <handler trigger="~/media/files/securefiles/" handler="sitecore_media_secure1.ashx" />

  </customHandlers>
</sitecore>

Is what I am trying even possible? Can you get Sitecore to use a custom MediaRequestHandler for a certain folder and then use the regular MediaRequestHandler for everything else?

I am also not really sure I understand how to set up the handlers in Web.Config. What is the path value? I don't understand what sitecore_media.ashx is. Can I just make up a different one like I did?

4
  • Did you try to move your custom handlers first, before the default -/media/ ones?
    – jammykam
    Commented Oct 23, 2019 at 21:37
  • Yeah I moved them to the top and it had no effect. Commented Oct 24, 2019 at 12:52
  • There are a couple of things I don't quite understand. First, what is sitecore_media.ashx? Is that a real file or endpoint somewhere? For my custom handler I just made up sitecore_media_secure1.ashx. Will that work? Second I notice that the prefix used out of the box is -/media/ - is this the same as /media library/? Is there a difference between /media/ and /media library/? Commented Oct 24, 2019 at 12:55
  • Did you ever get this resolved? I'm facing a very similar issue.
    – kiapnamor
    Commented Sep 23, 2020 at 16:29

1 Answer 1

1

Yes, it should be possible.

However it is not enough to use custom handler. There are two things that are required(it looks like you did only second part, as only second part is described in the link that you have shared):

  1. You need to override Sitecore.Resources.Media.MediaManager that will generate links to your custom handler if item is located in folder that you want. There is already question/answer how to do it.
  2. You need to create your own handler. Approach that is described in the article that you have linked in your question should work fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.