I'm trying to serve image files from a specific url system, like so:
mysite/Images/Category/File_Name.extension
I've set up a route like so:
routes.MapRoute( "ImagesRoute", // Route name
"Images/{category}/{file}.jpg", // URL with parameters
new { controller = "Posts",
action = "ViewImage",
category = "", file = "" } // Parameter defaults
);
Which is supposed to map to my controller action:
public ActionResult ViewImage(string category, string file)
{
var dir = Server.MapPath("/Images");
var imgtitle = file.Replace("_", " ").Replace(".jpg", "");
var repos = new BlogImagesRepository();
var guid = repos.FetchImageByCategoryAndTitle(category, imgtitle);
var path = Path.Combine(dir, category, guid.ImageGuid.ToString());
return File(path, "image/jpeg");
}
If I remove the .jpg extension from the route and request a file title without the .jpg extension on the url (ie: Images/MyCategory/My_Image) it displays just fine. However, adding the .jpg extension results in a 404 error--
The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.
I assume it's looking for the file, instead of the controller action.
Adding the .jpg to the routes did not resolve this; I'm unsure what to do, and my google luck hasn't been very high with similar questions I saw on here.
How can I do this for .jpg, and similar image types? Do I have to set up an ignore for that particular path somehow? If so, how do I go about doing that? The application is just being ran through VS 2012 currently.