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)
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();
}
}
}
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);
}
}
}
Protected Sub Application_Start()
RouteTable.Routes.Add(New Route("images/{*number}", New NumberRouteHandler()))
End Sub