简体   繁体   中英

Using ASP.NET routing to serve static files (part 2)

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.

Fixed by:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM