Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct practices for where to put images and static files in an ASP.NET MVC application?

Tags:

There is a directory in the standard ASP.NET template "Content" where most people seem to be putting in images and css files etc.

For instance stackoverflow's logo:

alt text
(source: stackoverflow.com)

actually is refered to with a server path containing 'content' in the URL (just do View Source for any SO page and you'll see this). So they obviously are storing images in "content/images/...".

src="/Content/Img/stackoverflow-logo-250.png" 

Edit: Sometime in the last 10 years they changed the path - but this is what it used to be.

I dont particularly like this. My HTML ends up with /content all over it, and its slightly harder to migrate existing pages that just have /image. Fortunately my stylesheet doesnt end up with content all over it, as long as I save it in content\site.css.

An alternative is to put an images directory in the root, but then you get images at the same level as Controllers and Views which is pretty horrible.

I'd wondered about trying to add a redirection rule like this :

routes.RedirectRoute(
   "images rule",
   "Images/{*}",
   "Content/Images/{1}");    // this is NOT valid code - I made it up

But that code doesnt work because I just made it up. I could use a third party redirection/rewriting plug-in but I want to keep everything 'pure' within the MVC model.

What has anyone else found in this area? Or is everyone just happy with an extra ("/content".length) bytes in their source for every image they serve.

like image 723
Simon_Weaver Avatar asked Jan 23 '09 23:01

Simon_Weaver


People also ask

Where do we store static content in an MVC application?

Static files are stored within the project's web root directory. The default directory is {content root}/wwwroot , but it can be changed with the UseWebRoot method. For more information, see Content root and Web root.

Where do images go in MVC?

Images are stored in /Content/Images.

What is the folder structure of ASP NET MVC?

The Views folder contains a subfolder for each Controller and Views for the Controller action results. The View's sub folder is named with the controller-name-prefix and View is named with the controller action. The Views folder contains a Web. config file.

What is App_Start folder in ASP NET MVC?

The App_Start folder of an ASP.NET MVC application contains configuration-related class files which are needed to be executed at the time of application starts. The classes like BundleConfig, FilterConfig, RouteConfig, IdentityConfig, etc are stored within this folder.


1 Answers

To be honest, I don't think its really something to worry about... "/Content" is going to make a pretty minimal contribution to your page size. If you still want to do it, here are some options:

Option 1, if you are running on your own server, is to check out the IIS URL Rewrite module: http://learn.iis.net/page.aspx/460/using-url-rewrite-module/

Option 2 is to use either RedirectResult, or ContentResult, to achieve the same effect in the MVC framework

First, map a "catchall" route under "Images/" to a controller action, like so

routes.MapRoute("ImageContent", 
                "Images/{*relativePath}", 
                 new { controller = "Content", action = "Image" })

Make sure it is above your standard "{controller}/{action}/{id}" route. Then, in ContentController (or wherever you decide to put it):

public ActionResult Image() {
    string imagePath = RouteData.Values["relativePath"]
    // imagePath now contains the relative path to the image!
    // i.e. http://mysite.com/Images/Foo/Bar/Baz.png => imagePath = "Foo/Bar/Baz.png"
    // Either Redirect, or load the file from Content/Images/{imagePath} and transmit it
}

I did a quick test, and it seemed to work. Let me know in the comments if you have any problems!

like image 170
Andrew Stanton-Nurse Avatar answered Sep 27 '22 18:09

Andrew Stanton-Nurse