1

Replace Sitecore.MediaRequestHandler in web configuration with a patch file inside the handlers.

Existing one:

`<add verb="*" path="sitecore_media.ashx" type="Sitecore.Resources.Media.MediaRequestHandler, Sitecore.Kernel" name="Sitecore.MediaRequestHandler" /`>

custom handler:

<add verb="*" path="sitecore_media.ashx" type="example.example.example.Processors.CustomMediaRequestHandler, example.example.Redirects" name="Sitecore.MediaRequestHandler"/>
2
  • Can you add more detail what issue you are facing or what are logs? Commented Jun 3 at 5:52
  • Hi, @GarimaThakore, I have implemented a redirect from media files to media files and it needs to override the existing handler on the web. Config file '<add verb="*" path="sitecore_media.ashx" type="Sitecore.Resources.Media.MediaRequestHandler, Sitecore.Kernel" name="Sitecore.MediaRequestHandler" /`>' created the custom configuration file, but it isn't working. Commented Jun 4 at 11:32

4 Answers 4

3

As the media handler is inside the Web.config you need to use transformation for Web.config and for this transformation you need to use SlowCheetah. Just like in the below image, I have created Web.Debug.config and Web.Release.config and both configs have different values/settings so as per your requirement you can create your configuration.

enter image description here

You can find more details here https://naveed-ahmad.com/tag/slowcheetah/

Previously I have faced an issue related to slow cheetah so you can check here as well in case you face the same problem.

1

The patch:instead attribute is used to replace the existing Sitecore.MediaRequestHandler processor in the initialize pipeline with your custom handler.

  1. Create a new patch file, for example, CustomMediaRequestHandler.config, in the App_Config/Include folder.

    <configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
      <sitecore>
        <pipelines>
          <initialize>
            <processor type="example.example.example.Processors.CustomMediaRequestHandler, example.example.Redirects" patch:instead="processor[@type='Sitecore.Resources.Media.MediaRequestHandler, Sitecore.Kernel']"/>
          </initialize>
        </pipelines>
      </sitecore>
    </configuration>
    

2.If your handler configuration is under the <handlers> section, you can use the following XML structure:

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/">
  <system.webServer>
    <handlers>
      <add verb="*" path="sitecore_media.ashx" type="example.example.example.Processors.CustomMediaRequestHandler, example.example.Redirects" name="Sitecore.MediaRequestHandler" patch:instead="add[@name='Sitecore.MediaRequestHandler']"/>
    </handlers>
  </system.webServer>
</configuration>

Hope this help!

0

You can override the out-of-the-box media handler with your own like below.

Example of code of the custom Media Request Handler

using System.Web;
using Sitecore.Resources.Media;

namespace MyCustomCode.Handlers
{
    public class MediaRequestHandler : Sitecore.Resources.Media.MediaRequestHandler
    {
        protected override bool DoProcessRequest(HttpContext context, MediaRequest request, Media media)
        {
            // Do your authentication and redirect logic here
            return base.DoProcessRequest(context, request, media);
        }
    }
}

And use a patch like this.

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <system.webServer>
        <handlers>
            <add verb="*" path="sitecore_media.ashx" type="MyCustomCode.Handlers.MediaRequestHandler, MyCustomCode" name="Sitecore.MediaRequestHandler" xdt:Transform="SetAttributes" xdt:Locator="Match(name)" />
        </handlers>
    </system.webServer>
</configuration>

Hope this helps.

1
  • I tried but it's not working, still getting the "ConfigurationException: An error occurred during applying the patch file:" error. Will it override the web.config file? Commented Jun 4 at 10:22
0

In order to patch your custom implementation for media handler inside Web.config, you need to create a transformation file.

<?xml version="1.0" encoding="utf-8"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
   <system.webServer>
      <handlers>
        <add verb="*" path="sitecore_media.ashx" type="{{CustomImplementation}}" name="Sitecore.MediaRequestHandler" xdt:Transform="Replace" xdt:Locator="Match(path)" />
      </handlers>
   </system.webServer>
</configuration>

You can use below link to set up SlowCheetah on your project, to configure transformation on Web.config file, this will create Web.Debug.config and Web.Release.config file on the solution.

https://www.jondjones.com/learn-sitecore-cms/sitecore-developers-guide/team-development-for-sitecore/setting-up-your-sitecore-project-to-use-config-transforms/

Hope this helps!!!

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.