How do I route images using ASP.Net MVC routing?
Solution 1:
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 "";
}
}
}
Solution 2:
If you were to do this using ASP.NET 3.5 Sp1 WebForms you would have to create a seperate ImageHTTPHandler that implements IHttpHandler to handle the response. Essentially all you would have to do is put the code that is currently in the GetHttpHandler method into your ImageHttpHandler's ProcessRequest method. I also would move the GetContentType method into the ImageHTTPHandler class. Also add a variable to hold the name of the file.
Then your ImageRouteHanlder class looks like:
public class ImageRouteHandler:IRouteHandler
{
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
string filename = requestContext.RouteData.Values["filename"] as string;
return new ImageHttpHandler(filename);
}
}
and you ImageHttpHandler class would look like:
public class ImageHttpHandler:IHttpHandler
{
private string _fileName;
public ImageHttpHandler(string filename)
{
_fileName = filename;
}
#region IHttpHandler Members
public bool IsReusable
{
get { throw new NotImplementedException(); }
}
public void ProcessRequest(HttpContext context)
{
if (string.IsNullOrEmpty(_fileName))
{
context.Response.Clear();
context.Response.StatusCode = 404;
context.Response.End();
}
else
{
context.Response.Clear();
context.Response.ContentType = GetContentType(context.Request.Url.ToString());
// find physical path to image here.
string filepath = context.Server.MapPath("~/images/" + _fileName);
context.Response.WriteFile(filepath);
context.Response.End();
}
}
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 "";
}
#endregion
}