KB's

How to filter the default ContentHttpHandler by extension

The following example shows a situation where “.mp3” files have been uploaded through the “Images and Documents” module.

Scenario:

You want to upload myfile.mp3 through the “Images and Documents” module.  Then, you want to link this item to an email, IM or a page.

 
Problem:

Currently, this is not possible because you could not  use “ .mp3” extension in the link.  This is due to the fact that the uploaded files are all stored in a database and ASP.NET handles the requests for the files ending in .sflb.

 

Solution:

To solve the issue you need to create a class which extends the ContentHttpHandler base class and overrides the ProcessContentItem method.

To achieve this follow the steps below:

 

1. Add ”App_Code”  folder to  your project root.

2. Add a new class called CustomCmsContentHandler.cs

using System; 
using System.Data; 
using System.Configuration; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using Telerik.Cms.Engine; 
using System.Web.Hosting; 
 
 
/// <summary> 
/// Summary description for CustomCmsContentHandler 
/// </summary> 
 
public class CustomCmsContentHandler : ContentHttpHandler 
    protected override void ProcessContentItem(IContent content) 
    { 
       string extension = VirtualPathUtility.GetExtension(HttpContext.Current.Request.Url.AbsolutePath); 
       if (extension.Equals(".mp3", StringComparison.OrdinalIgnoreCase) 
            && !content.MimeType.StartsWith("audio/")) 
       { 
            throw new HttpException(404, "Not found"); 
            return
       } 
       base.ProcessContentItem(content); 
   } 

3. Add the ISAPI filter for .mp3 extension to the IIS configuration (Take a look at our UserManual, section Configuring IIS). This will modify your web.config as follows:

<add name="mp3handler" path="*.mp3" verb="*" modules="IsapiModule" scriptProcessor="C:\Windows\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll" resourceType="Unspecified" preCondition="classicMode,runtimeVersionv2.0,bitness32" /> 

4. Declare the following httphandler in the <httpHandlers> section of the project’s  web.config.

<add verb="GET" path="*.mp3" type="CustomCmsContentHandler, App_Code" /> 

Note:

You can change this example so that other file extensions could be handled. All you need to do is to specify the MIME type for the selected extension in the “if” loop of ProcessContentItem method.

For example, the following should be defined for .jpg extension:

!content.MimeType.StartsWith("image/"