3

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:

  1. 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]/...

  2. 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, but Images/abc/level1/level2/level3/img.jpg will be handled

Why? no idea.

1 Answer 1

2

Fixed by:

routes.Add("Images Specials", new Route("Images/{folder}/{*filename}", null, new RouteValueDictionary { { "outgoing", new ImageRouteConstraint() } }, new ImageRouteHandler()) );

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.