24

There something I still do not understand about how the Content folder works in ASP.NET MVC. To make things clearer I have a few questions:

  1. Is the Content folder the root folder? I mean does http://localhost/ point to Content or is it something else?
  2. I have a file named dummyIcon.png inside Content/images/temp folder. How do I locate it from my domain layer (which is a Code Library project)?
  3. What is the best practice of displaying images in ASP.NET MVC? Should I store a path to the image in the database (which I personally prefer), or do I save a byte array and return it to the view?

I found the following links to be helpful within the context of the MVC web application, but I'd still appreciate some answers to the questions posted above. Thank you.

Can an ASP.NET MVC controller return an Image?

how to display image using view and controller by ASP.NET MVC

Community
  • 1
  • 1
Kassem
  • 8,116
  • 17
  • 75
  • 116

3 Answers3

40
  1. Anything in the root will point to the root if it is ignored by your routes:

    If you have an image placed on the on the root of your project. Then, say http:://localhost/dummy.ico" will give you a 404, no controller found. Until you do this in your global.asax.cs:

    routes.IgnoreRoute("dummy.ico");
    //you could add wildcards here to match different things
    
  2. From Code if you use says File.Open(); you need the physical path to the file. You get it like this:

    string filePath = Server.MapPath(Url.Content("~/Content/Images/Image.jpg"));
    
  3. It is upto you here, although I would say, putting files into the database makes a lot of sense if you want everything in one place. If you need to move your app around you would just move the data base.

When it comes to file paths, please remember you don't want duplicate file names, so you will have to give each file a GUID and then link it up. It could make sense if you have a large number of files (or large files itself) so you're database won't grow like crazy.

HTH

gideon
  • 19,329
  • 11
  • 72
  • 113
  • +1 for the tip on using a GUID. I forgot about that. Yes, there are going to be a lot of images being uploaded to the site. The site is an auction website, and users will upload a max of 3 images of the item they are posting. I did not understand your last sentence, are you talking about placing the images inside the database or in a directory? – Kassem Mar 24 '11 at 10:52
  • 1
    @Kassem like I said, its upto your, a large number of files, I would say store the files all in one folder with guids for names and in your database you don't store the path. You store the GUID only, so migrating, moving will be easier. (If your images are small/ or a small number it would make sense to just stuff them into the DB, thats what I mean't) – gideon Mar 24 '11 at 10:58
12

1.Is the Content folder the root folder? I mean does http://localhost/ point to Content or is it something else?

No, http://localhost:port/ does not point to content folder. You can access files in content folder through http://localhost:port/content/...

2.I have a file named dummyIcon.png inside Content/images/temp folder. How do I locate it from my domain layer (which is a Code Library project)?

You should be able to access it as http://localhost:port/Content/images/temp/dummyIcon.png

3.What is the best practice of displaying images in ASP.NET MVC? Should I store a path to the image in the database (which I personally prefer), or do I save a byte array and return it to the view?

Where you store the images depends on your application needs. Are these generic images that is used to display application images (icons, company logo, etc).. Then it is best to store them in file system.

If your application deals with images and you work on storing images, manipulation etc, then you may need DB. I think, storing images used on the web application is a overhead.

Abdel Raoof Olakara
  • 19,223
  • 11
  • 88
  • 133
  • Oh it was the port number that I was missing! Thanks for pointing that out! :) And I also forgot to map the path using AutoMapper. Now I got it all sorted out! But about storing images inside the DB, wouldn't that make the size of the DB increase dramatically? – Kassem Mar 24 '11 at 10:29
  • yes. Its a trade-off you will have to consider.. there are lots of articles / debates about storing imgs in DB.. you should google some – Abdel Raoof Olakara Mar 24 '11 at 10:33
0

You should make a model object for your controller to return. in this example i am returning SearchPageModel, a class i have created. but lets say this object has a property called imageURL

but make sure the controller actually returns an ActionResult

so for example...

    [AcceptVerbs(HttpVerbs.Get)]
    public ActionResult Search()
    {
        SearchPageModel Model = new SearchPageModel();

        // populate the Model properties
        Model.ImageURL = "myjpeg"

        return View("Search", Model);
    }

i then pass this model object back to my desired view in this case my "Search" View

and to display the image, in the view i would add..

<img src="Images/<%=Model.ImageURL %>.jpg" />
JGilmartin
  • 8,683
  • 14
  • 66
  • 85
  • Yup, this is what I'm doing. But that's not really my question. What I'm asking for is how to map the path to an image inside my code and then store it in the database. Or, if there's some icon that's never going to change, I could store it inside the Content/images/icons folder and retrieve it from the View. – Kassem Mar 24 '11 at 10:14
  • 1
    i would always store on the file system and use the DB to save the paths – JGilmartin Mar 24 '11 at 10:31