Letting ASP.NET Handle Image File Extensions in IIS 6

Posted by: Jason N. Gaylords Blog, on 04 May 2009 | View original | Bookmarked: 0 time(s)

One of the best features of ASP.NET (either webforms or MVC) is its extensibility. One way you can extend ASP.NET is by using HttpHandlers or HttpModules. IIS 6 and IIS 7+ evaluate HttpHandlers and HttpModules slightly different. Dave Donaldson, an employee at Telligent, posted about the differences on his blog back in April of 2008.

I was recently working on a project that needed to serve up images using the actual image file extension (ie: .jpg, .gif, .png, etc) to allow for the images and URLs to be search engine optimized (SEO). Some of the images in the site were stored dynamically and some werent. For instance, the logo and background images were static for the web application. However, one of the application features included some basic CMS functionality to add, update, and remove employee photos. I choose an HttpModule so that I can look at each request that is being processed to determine how it should be handled (note: Ive used the same module to handle other functionality besides just images). Heres an example of what the HttpModule looks like:

public class ImageProcessingModule : IHttpModule
{
    public void Init(HttpApplication context)
    {
        context.BeginRequest += this.app_BeginRequest;
    }

    public void Dispose() { }

    public void app_BeginRequest(object s, EventArgs e)
    {
        HttpApplication application = (HttpApplication)s;
        HttpContext context = application.Context;
        string filePath = context.Request.FilePath;
        string urlPath = context.Request.Path;

        if (context.Request.ApplicationPath != "/")
        {
            urlPath = urlPath.Replace(context.Request.ApplicationPath, "");
        }

        string fileExt = VirtualPathUtility.GetExtension(filePath).Replace(".", "");

        // Let's check to see if the path ends with an image extension and is 
        // "dynamic". If the image is contained in one of our themes, then it should
        // be treated as a static image.
        if (IsImage(urlPath) && !urlPath.Contains("/App_Themes/"))
        {
            // TODO: Rewrite the path to handle the image properly.
            context.RewritePath("NEW PATH", false);
        }
    }

    private Boolean IsImage(String path)
    {
        if (path.EndsWith(".jpg") | path.EndsWith("gif"))
            return true;

        return false;
    }
}

Now the module rewrites the path for any file that contains a .jpg or .gif extension. However, we still need to look to handle the static files properly. At this point, IIS6, which handles web requests differently, does not know how to process .jpg or .gif images. So, by changing the conditional statement like so:

// Let's check to see if the path ends with an image extension and is 
// "dynamic". If the image is contained in one of our themes, then it should
// be treated as a static image.
if (IsImage(urlPath) && !urlPath.Contains("/App_Themes/"))
{
    // TODO: Rewrite the path to handle the image properly.
    context.RewritePath("NEW PATH", false);
}
else if (IsImage(urlPath))
{
    context.Response.TransmitFile(filePath);
}

We can now let IIS6 handle our image files.

Advertisement
Free Agile Project Management Tool from Telerik
TeamPulse Community Edition helps your team effectively capture requirements, manage project plans, assign and track work, and most importantly, be continually connected with each other.
Category: IIS | Other Posts: View all posts by this blogger | Report as irrelevant | View bloggers stats | Views: 1700 | Hits: 16

Similar Posts

  • Problems running Windows Communication Foundation (.svc) on an upgraded Windows 7 and IIS 7 more
  • Web Deployment Tool released to web (RTW) more
  • Clean Web.Config Files (VS 2010 and .NET 4.0 Series) more
  • Range-Specific Requests in ASP.NET more
  • How to download Internet Explorer 8 for Windows 7 E without any Web browser? more
  • Using the RadFileExplorer for ASP.NET AJAX in a MOSS web application more
  • Are you caching your images and scripts? IIS SEO can tell you more
  • NDepend and CruiseControl.NET more
  • Don’t use Response.End() with OutputCache more
  • Creating a folder inside the ZIP file with System.IO.Packaging more

News Categories

.NET | Agile | Ajax | Architecture | ASP.NET | BizTalk | C# | Certification | Data | DataGrid | DataSet | Debugger | DotNetNuke | Events | GridView | IIS | Indigo | JavaScript | Mobile | Mono | Patterns and Practices | Performance | Podcast | Refactor | Regex | Security | Sharepoint | Silverlight | Smart Client Applications | Software | SQL | VB.NET | Visual Studio | W3 | WCF | WinFx | WPF | WSE | XAML | XLinq | XML | XSD