ASP.NET MVC Routing for files with muliple sub-directories
I need to setup a file handler to route with multiple sub directories something like tihs;
http://localhost/images/7/99/786936215595.jpg
I tried putting this in the global.asax file;
routes.Add(
"ImageRoute",
new Route("covers/{filepath}/{filename}",
new ImageRouteHandler()));
I am using the ImageHandler found in this Question, which works great if you have a single sub-directory (ie '/images/15/786936215595.开发者_运维百科jpg') but fails when you have multiple directories.
I tried setting up a wildcard and that didnt work (ie 'new Route("covers/{filepath}/*/{filename}"')
This is serving images from a large NAS (think something like 3 million images) so its not like I can just move files around.
Thanks!
Ok after much playing around and google fu I found how to make it work.
Change the route definition like this;
routes.Add(
"ImageRoute",
new Route("images/{*filepath}",
new ImageRouteHandler()));
Then put this after the default MapRoute. The important part is the "*" before the filepath, tells MVC to send anything following this as part of the filepath RouteData. So in the GetHttpHandler() method I can get the full path by using this;
string fp = requestContext.RouteData.Values["filepath"] as string;
Woot!
Can't you treat the entire path as one route parameter? Like so:
routes.Add(
"ImageRoute",
"/images/{path}",
new { controller = "Image", action = "Image" }
);
And then access the entire path in the ActionResult Image(string path) { }
action method?
精彩评论