0

I have setup a routing table in the global.asax file for images that have been moved to the database. When I use the url such that EmpImages/[numeric id] as a basic format, it works fine if I use the url that does that same ~/EmpImages/42, but we have hundreds of hardcoded links that are ~/EmpImages/42.png. When I try to use the EmpImages/[numeric id].png, the handler is never called.

I have looked at several samples that show the .ext, but they are using page routes instead of handlers. With the code below, can you tell me what I'm missing?

This part works:

RouteTable.Routes.Add(new Route("EmpImages/{id}/{size}", new EmployeeImageRouteHandler()));
RouteTable.Routes.Add(new Route("EmpImages/{id}", new EmployeeImageRouteHandler()));

When using the URL:

~/EmpImages/42
~/EmpImages/42/256

But when I try:

RouteTable.Routes.Add(new Route("EmpImages/{id}/{size}.png", new EmployeeImageRouteHandler()));
RouteTable.Routes.Add(new Route("EmpImages/{id}.png", new EmployeeImageRouteHandler()));

When using the URL:

~/EmpImages/42.png
~/EmpImages/42/256.png

It fails. The handler is never called.

What simple thing am I missing?

Gary Smith
  • 61
  • 1
  • 10
  • What other routes are you specifying? Alone this type of route should pose no problem. The request is probably matching a route registered before your Image handler in your second example. Try moving it to the very first registered route and see if that helps. – agradl Jul 27 '12 at 18:16
  • Currently those are the only two routes. When I used the first example that works with /EmpImages/42 and append /EmpImages/42.png to it, I would expect it to still trigger the handler, but it doesn't in that case either. I did read somewhere that '.''s are special characters but elsewhere in the microsoft documentation they show samples, specifically with .jpeg, but they route to a page, not a handler. Creating a seperate web page might be a work around, but the handler should in theory work – Gary Smith Jul 27 '12 at 18:37
  • See the edit to my answer below. – dmi_ Jul 30 '12 at 16:23

3 Answers3

1

According to the documentation for Route, it seems that the way you are specifying the route is unsupported.

Quoting the docs:

The URL pattern consists of segments that come after the application name in an HTTP request... Each segment is delimited by the / character. When a segment is enclosed in braces ( { and } ), the segment is referred to a URL parameter. ASP.NET routing retrieves the value from the request and assigns it to the URL parameter... If the segment is not enclosed in braces, the value is treated as a literal value.

Apparently, it does not support segments with mixed URL parameters and literals.

You need to have your EmployeeImageRouteHandler internally deal with the ".png" extension of the size parameter with string processing.

EDIT: In addition to this point, it seems that there is a known problem in handling URLs with dots see this StackOverflow question . The solution proposed there is to include

<httpRuntime relaxedUrlToFileSystemMapping="true" />

in your web.config, but this works only for ASP 4.0 and IIS 7.0 and above. The details of this problem, as mentioned in the indicated question, are discussed on Haacked.

Community
  • 1
  • 1
dmi_
  • 1,187
  • 2
  • 12
  • 26
  • When I use the normal handler that works, using the .png (or any extension for that matter), the handler is simply not triggered. I'd be happy to strip off the trailing png/jpeg/whatever, but I don't even get that far. – Gary Smith Jul 27 '12 at 18:39
  • Hmm. Can you load regular (real) .png images from your site? Perhaps your server is configured to ignore such requests (like requests for .master or .config files) – dmi_ Jul 30 '12 at 16:11
  • Yes, this is a special-case handling issue. Apparently IIS sees the dot, assumes the request is for a real file, and doesn't pass it to ASP.NET. See the edit in the answer – dmi_ Jul 30 '12 at 16:24
0

As far as I know, dmi_ above is right about route parses limitations. But you can draw a simple workaround from there:

Register your routes as folows:

RouteTable.Routes.Add(new Route("EmpImages/{id}/{size}/i.png", new EmployeeImageRouteHandler()));
RouteTable.Routes.Add(new Route("EmpImages/{id}/i.png", new EmployeeImageRouteHandler()));

And request them:

~/EmpImages/42/i.png
~/EmpImages/42/256/i.png
Community
  • 1
  • 1
Sergei Rogovtcev
  • 5,804
  • 2
  • 22
  • 35
0

The issue is that the path you're attempting to catch with routing is a static resource. Routing is ignored if a file exists in the location of the route. This is by design and to both prevent conflicts between static url references and prevent the routing engine from being engaged for every single request.

Try putting a break point in your handler and then specifying an id that you know does not exists to see if this is the case.

agradl
  • 3,536
  • 2
  • 18
  • 16