Search code examples
c#url-routingasp.net-routing

How do I route images through ASP.NET routing?


I'd like to create a dynamic thumbnail resizer so that you can use the following URL to get a resized image:

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

I tried setting up a route like this:

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

But if the file exists at the URL, ASP.NET will bypass the routing and return you just the file instead. How do I force ASP.NET to route the images instead of returning what's on disk?


Solution

  • Thats how asp.net routing works, there is no away around that... you have to use Rewrite if you want to intercept requests for existing files.

    Update

    Seems like i was a bit too fast on the trigger there. There seems to be a property you can set which allows you to enforce a route even for existing files.

    RouteCollection.RouteExistingFiles Property

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

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