Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I route images through ASP.NET routing?

I'd like to create a dynamic thumbnail resizer so that you can use the following URL to get a resized image:

http://server/images/image.jpg?width=320&height=240

I tried setting up a route like this:

routes.MapRoute(null,
                "{filename}",
                new { controller = "Image", action = "Resize" });

But if the file exists at the URL, ASP.NET will bypass the routing and return you just the file instead. How do I force ASP.NET to route the images instead of returning what's on disk?

like image 328
Daniel T. Avatar asked Dec 18 '10 01:12

Daniel T.


People also ask

How do I use routes in MapRoute?

You can also configure a custom route using the MapRoute extension method. You need to provide at least two parameters in MapRoute, route name, and URL pattern. The Defaults parameter is optional. You can register multiple custom routes with different names.

What is route in asp net?

The ASP.NET Routing module is responsible for mapping incoming browser requests to particular MVC controller actions. By the end of this tutorial, you will understand how the standard route table maps requests to controller actions.

What is right way to define routes MapRoute () in MVC?

Routing in ASP.NET MVC cs file in App_Start Folder, You can define Routes in that file, By default route is: Home controller - Index Method. routes. MapRoute has attributes like name, url and defaults like controller name, action and id (optional).

Where is route mapping code in ASP.NET MVC?

Where is the route mapping code written? The route mapping code is written in "RouteConfig. cs" file and registered using "global. asax" application start event.


2 Answers

Why not just use an action to do this? A controller's action can stream back an image. Otherwise, the typical way, say with ASPX, is that a handler or handler factory listens for the file extension and processes it accordingly. Or use URL rewriting to rewrite the URL in the request.

like image 137
Brian Mains Avatar answered Oct 28 '22 00:10

Brian Mains


Thats how asp.net routing works, there is no away around that... you have to use Rewrite if you want to intercept requests for existing files.

Update

Seems like i was a bit too fast on the trigger there. There seems to be a property you can set which allows you to enforce a route even for existing files.

RouteCollection.RouteExistingFiles Property

http://msdn.microsoft.com/en-us/library/system.web.routing.routecollection.routeexistingfiles.aspx

Gets or sets a value that indicates whether ASP.NET routing should handle URLs that match an existing file. True if ASP.NET routing handles all requests, even those that match an existing file; otherwise, false. The default value is false.

like image 35
Pauli Østerø Avatar answered Oct 28 '22 01:10

Pauli Østerø