Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a problem, I made an imagehandler (ashx file) which works fine.

But now I would like to have search engine friendly url to the pictures:

www.site.dk/imagehandler.ashx?number=2255
becomes:
www.site.dk/images/2255

My problem is that Route in Global.asax dosen works with ashx files!!!

I've searched a lot online (3 days now) and found several examples that work, but they all write in c# and I'm working in vb (vs. 2010)
When I try to translate the codes to VB (I use Instant_VB) then codes is full of errors.

I hope that there is another who has had the same problem ... who can show a working sample in VB.

Below is a code that I can get to work in C#: (here is ashx file turned into a cs file - that ok)
C#
//****************************************** NumberRouteHandler.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
namespace Web
{
    public class NumberRouteHandler : IRouteHandler
    {
        public IHttpHandler GetHttpHandler(RequestContext requestContext)
        {
            return new NumberToImageHandler();
        }
    }
}
//****************************************** NumberToImageHandler.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
namespace Web
{
    public class NumberToImageHandler : ImageHandler
    {
        public NumberToImageHandler()
        {
            base.ContentType = ImageFormat.Png;
            base.EnableClientCache = false;
        }
public override ImageInfo GenerateImage(System.Collections.Specialized.NameValueCollection parameters)
        {
            string numberText = HttpContext.Current.Request.RequestContext.RouteData.Values["number"] as string;
            if (string.IsNullOrEmpty(numberText))
                numberText = "1";
            MemoryStream ms = new MemoryStream(File.ReadAllBytes("C:\\z2.jpg"));
            var img = Image.FromStream(ms);
          return new ImageInfo(img);
        }
    }
}
//****************************************** Global.asax
Protected Sub Application_Start()
    RouteTable.Routes.Add(New Route("images/{*number}", New NumberRouteHandler()))
End Sub
Posted
Updated 8-May-11 6:07am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900