I upgraded my site to use ASP.Net MVC from traditional ASP.Net webforms. I'm using the MVC routing to redirect requests for old .aspx pages to their new Controller/Action equivalent:
routes.MapRoute( "OldPage", "oldpage.aspx", new { controller = "NewController", action = "NewAction", id = "" } );
This is working great for pages because they map directly to a controller and action. However, my problem is requests for images - I'm not sure how to redirect those incoming requests.
I need to redirect incoming requests for http://www.domain.com/graphics/image.png to http://www.domain.com/content/images/image.png.
What is the correct syntax when using the .MapRoute()
method?
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.
In MVC, routing is a process of mapping the browser request to the controller action and return response back. Each MVC application has default routing for the default HomeController. We can set custom routing for newly created controller. The RouteConfig.
Configure a Route Every MVC application must configure (register) at least one route configured by the MVC framework by default. You can register a route in RouteConfig class, which is in RouteConfig. cs under App_Start folder.
The RouteCollection class provides methods that enable you to manage a collection of objects that derive from the RouteBase class. Typically, you will use the static Routes property of the RouteTable class to retrieve a RouteCollection object. The Routes property stores all the routes for an ASP.NET application.
You can't do this "out of the box" with the MVC framework. Remember that there is a difference between Routing and URL-rewriting. Routing is mapping every request to a resource, and the expected resource is a piece of code.
However - the flexibility of the MVC framework allows you to do this with no real problem. By default, when you call routes.MapRoute()
, it's handling the request with an instance of MvcRouteHandler()
. You can build a custom handler to handle your image urls.
Create a class, maybe called ImageRouteHandler, that implements IRouteHandler
.
Add the mapping to your app like this:
routes.Add("ImagesRoute", new Route("graphics/{filename}",
new ImageRouteHandler()));
That's it.
Here's what your IRouteHandler
class looks like:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.Compilation; using System.Web.Routing; using System.Web.UI; namespace MvcApplication1 { public class ImageRouteHandler : IRouteHandler { public IHttpHandler GetHttpHandler(RequestContext requestContext) { string filename = requestContext.RouteData.Values["filename"] as string; if (string.IsNullOrEmpty(filename)) { // return a 404 HttpHandler here } else { requestContext.HttpContext.Response.Clear(); requestContext.HttpContext.Response.ContentType = GetContentType(requestContext.HttpContext.Request.Url.ToString()); // find physical path to image here. string filepath = requestContext.HttpContext.Server.MapPath("~/test.jpg"); requestContext.HttpContext.Response.WriteFile(filepath); requestContext.HttpContext.Response.End(); } return null; } private static string GetContentType(String path) { switch (Path.GetExtension(path)) { case ".bmp": return "Image/bmp"; case ".gif": return "Image/gif"; case ".jpg": return "Image/jpeg"; case ".png": return "Image/png"; default: break; } return ""; } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With