0

Current I have an image folder in one of my Areas as such: ~/Areas/AreaName/Images/

So in order to display an image, I have to run through: url.com/Areas/AreaName/Images/Image.jpg

However, for sake of cleanliness, I would like to route the URL AreaName/Images/Image.jpg to the actual location at: ~/Areas/AreaName/Images/Image.jpg.

How can I do this in the AreaRegistration? I understand how to map a URL pattern to a controller / action, etc. but not how to map a folder to another. I may be confusing myself by using AreaRegistration in the first place because this situation doesn't involve a controller / action, but just a location of images.

As far as I can tell, there's no overload for what I'm trying to do in context.MapRoute().

Any direction is appreciated.

1 Answer 1

2

By default when a request for a file (in this case Image.jpg) comes, IIS will directly serve it from the location. It will not go through the MVC request pipeline. So there is nothing much you can do in the code configuration to change the path/url.

You have 2 options

  1. Move the static content to the app root, where you can organize it in a folder which is matching to the area name (or whatever name you like) and refer that in the views from those areas.

  2. Render the images via code. In this approach, you can write an action method which accepts a unique identifier for the file (Ex: unique file name/id) and read the file using code and return a FileResult. Since you are writing code to read file, you can read it anywhere from the application. You can use attribute routing to definie pretty (nice looking) url pattern for this action method.

Here is a simple example to return image from the folders. Please feel free to improve it as needed and use it.

[System.Web.Mvc.Route("Images/{id}/")]
[System.Web.Mvc.Route("Images/{area}/{id}")]
public ActionResult GetImage(string id,string area = "")
{
    var webRoot = Path.Combine(Path.Combine("~/Contents/Img/", area), id) + ".png";
    return File(webRoot, "image/png");
}

Assuming you have an image called logo.png in ~/Contents/Img directory (for non area related pages) and another one specific to your Billing area in ~/Contents/Img/Billing, You can request them like

yourSiteName/Images/billing/logo

yourSiteName/Images/logo

I would go for option 1 as i would not want the requests for images to go via the MVC request pipeline (unless i want to do some fancy thing for the response like resizing based on the request etc). Also you get the IIS level caching as well. But hey, pick the one suitable for your use case.

Sign up to request clarification or add additional context in comments.

3 Comments

I can't believe I overlooked something as simple as option 1. Although it adds a bit of confusion with duplicate folder names in the project, I have to sacrifice that for efficiency. Thanks!
Actually, option 1 causes an issue where it thinks I'm trying to browse the directory I created for the Images under ~/AreaName/ when I really want the Default mapped route for the Area when I navigate to url.com/AreaName. Is there any way around this? I don't have anything in the root directory ~/AreaName/ except for the /AreaName/Images/ folder.
I just added some sample code if you want to go with option 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.