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, but Images/abc/level1/level2/level3/img.jpg
will be handled
Why? no idea.
Fixed by:
IRouteConstraint
. That will 2 two things... only accept IncomingRequest
, and check if the folder is abc or xyz. Source: After add MapPageRoute to an asp.net mvc project, the site stops to enter in Home Controller routes.Add("Images Specials", new Route("Images/{folder}/{*filename}", null, new RouteValueDictionary { { "outgoing", new ImageRouteConstraint() } }, new ImageRouteHandler()) );
ImageHandler
to make looking better. Source: http://weblogs.asp.net/fredriknormen/archive/2007/11/18/asp-net-mvc-framework-create-your-own-iroutehandler.aspx , and Dynamically Rendering asp:Image from BLOB entry in ASP.NET
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.