I had similar problem as presented here: Using ASP.NET routing to serve static files
In RouteConfig, I added the following lines:
routes.Add( "Images ABC", new Route("Images/abc/{*filename}", new ImageRouteHandler("abc")) );
routes.Add( "Images XYZ", new Route("Images/xyz/{*filename}", new ImageRouteHandler("xyz")) );
I found a pretty decent implementation of ImageRouteHandler
here: http://www.phpvs.net/2009/08/06/aspnet-mvc-how-to-route-to-images-or-other-file-types/, I just added a parameter in the ctor to build the physical path...
Constraint: I have other paths in Images
, beside of ABC or XYZ, that I don't want to be routed.
NOTE: I use {*filename}
so I can refer to multiple segments... more info here: http://msdn.microsoft.com/en-us/library/cc668201%28v=vs.100%29.aspx#handling_a_variable_number_of_segments_in_a_url_pattern
Questions:
Can I combine these 2 routes in 1 single statement without violate the constraint? maybe using somekind of regular expression such as
Images/[abc|xyz]/...
The position matters. below or above the default routing.
routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
a) if the code is placed above of default routing, it will mess up the RedirectToAction
b) and if the code is placed below the default routing, it won't handle the immediate routing, ex.
Images/abc/img.jpg
won't be handled, butImages/abc/level1/level2/level3/img.jpg
will be handled
Why? no idea.