Search code examples
asp.netroutesasp.net-4.0asp.net-routing

ASP.NET 4 Web forms Routing images


I'm using ASP.NET 4 Web forms routing, for example like this:

routes.MapPageRoute("page-browse", "{Language}/{Label}", "~/Default.aspx")

So the webadress could look like: http://localhost/mywebsite/eng/home

In the root of my website I have a folder "Images".

Image display works when I'm in the root of my website, e.g. by using http://localhost/mywebsite/default.aspx

But when using routing it doesn't work, because the image relative url will look at http://localhost/mywebsite/eng/images instead of http://localhost/mywebsite/images

Is there a way to prevent this using ASP.NET 4 Routing mechanism? Or is the only way to use absolute url's to images?


Solution

  • Two things you could try.

    1) Set RouteExistingFiles to false. This will stop routing on any file that the server matches as already existing. Only "virtual" urls that match a route will actually be routed:

    routes.RouteExistingFiles = false;
    

    2) Use a StopRoutingHandler route. For example, this would stop routing on all jpgs. You could also set it up to ignore the entire images directory.

    routes.Add(new Route("*\.jpg", new StopRoutingHandler()));