How to redirect images through ASP.NET routing?

I would like to create a dynamic thumbnail editor so that you can use the following URL to get the modified image:

http://server/images/image.jpg?width=320&height=240 

I tried to configure the route as follows:

 routes.MapRoute(null, "{filename}", new { controller = "Image", action = "Resize" }); 

But if the file exists in the URL, ASP.NET bypasses the routing and instead returns only the file to you. How to force ASP.NET to route images instead of returning what is on disk?

+6
c # url-routing asp.net-routing
source share
5 answers

This is how asp.net routing works, there is nothing around ... you need to use Rewrite if you want to intercept requests for existing files.

Update

Looks like I was too fast on the trigger. There seems to be a property that you can set that allows you to use the route even for existing files.

Property RouteCollection.RouteExistingFiles

http://msdn.microsoft.com/en-us/library/system.web.routing.routecollection.routeexistingfiles.aspx

Gets or sets a value indicating whether ASP.NET routing should process URLs that match an existing file. True, if ASP.NET routing processes all requests, even those that match an existing file; otherwise false. The default value is false.

+2
source share

Why not just use an action to do this? The action of the controller can send the image back. Otherwise, a typical way, say using ASPX, is that the factory handler or handler listens to the file extension and processes it accordingly. Or use URL rewriting to rewrite the URL in the request.

+4
source share

You may also consider:

  • Writing a module to process these image routes before routing it (registered with Web.Config)
  • Create your own route handler to process these images.

Both will allow you to remove the need to write as a controller, I think it is cleaner.

A very simple example of your own route handler (from memory) ...

Register as a regular route:

 /* Register in routing */ routes.Add("MyImageHandler", new Route("my-custom-url/{folder}/{filename}", new ImageRouteHandler()) ); /* Your route handler */ public class ImageRouteHandler : IRouteHandler { public IHttpHandler GetHttpHandler(RequestContext requestContext) { string filename = requestContext.RouteData.Values["filename"] as string; string folder = requestContext.RouteData.Values["folder"] as string; string width = requestContext.HttpContext.Request.Params["w"] as string; string height = requestContext.HttpContext.Request.Params["h"] as string; // Look up the file and handle and return, etc... } } 

I hope for this help. Many ways to expand and achieve :)

+2
source share

The easiest way is to route all the images through the controller and store the images in a separate place.

 routes.MapRoute("Images", "/images/{filename}", new { controller = "Image", action = "Resize" }); /sitebase/images/image.jpg //public image location /sitebase/content/images/image.jpg //real image location 

Then your controller will see which image is requested and download the corresponding file from the file system. This will allow you to do what you want without any special processing.

+1
source share

What about:

 routes.MapRoute("Images", "/images/{filename}.jpg", new { controller = "Image", action = "Resize" }); 

This should ensure that only URLs with a .jpg extension will match this route and are routed accordingly.

Also remember that you want to add your actions in the order most specific to the least specific, with the last added by default.

Of course, your action still needs to serve the image using the fileententresult file.

0
source share

All Articles