2

For example, I have an Asp.net MVC application in the path D:/myprojects/sample and I have placed static files in E:/files.

I don't want to place static files in content folder since static files are very large in size. Is it possible to use files in E:/files in my application? How?

I want to display static html files inside frame as below

&lt iframe id="content-frame" src="E:/files/index.html" width="100%" frameborder="0">

But i am getting alert in Firefox as "Firefox doesn't know how to open this address, becuase the protocol (e) is not associated with any program".

Will this issue be resolved if virtual directory is created in IIS?

Thanks for the replies.

sharmila
  • 39
  • 3
  • 7

4 Answers4

3

You can store files wherever you want on the file system, as long as you have permission. However, it often makes sense to save them in the application directory.

A good place is the App_Data folder in the root. The reason for this is because files in the App_Data folder cannot be accessed directly over HTTP - the server returns a 404 error. This could be useful if you need to restrict access to these files in the future to authorized users only for example.

Since you're using MVC, you'd need to set up a route to server files in this folder, which would look like:

public FilePathResult GetFile(string path)
{
    return File(Path.Combine(Server.MapPath("~/App_Data/"), path), "image/jpeg");
}

So then a file could be used in your markup like:

@Url.Action("GetFile", new { controller = "Files", path = "myimage.jpg" })

You probably don't want to pass the file path directly as this is just an example (maybe pass an id in instead) but you get the idea of how it would work.

Ian Routledge
  • 4,012
  • 1
  • 23
  • 28
  • +1 for some clear examples, and good explanation. I too would recommend setting up a route, and using the ~ convention. Else you could run into problems when you deploy. – IAmGroot Jan 17 '12 at 09:59
3

It really depends on the nature of these 'static' files and what a client, or indeed you, want to do with them.

If they're only to be accessed on the server, then it doesn't matter, so long as the identity the process runs as has the necessary permissions.

If they're to be accessed by the client, then you need to serve those files up.

If they're HTML files, then you can dump them in any folder you want. The Content folder seems a reasonable place to me so I'm interested to know why you "don't want to".

Images and other stuff, well there are already folders for that too of course.

But then you have to consider whether these static files are updated outside of a website deployment process. If so, then consider a simple virtual directory in IIS pointing to the location where those files are updated in situ - that way you don't have to worry about them.

Use routing and passing the request through a controller as a last resort, in my opinion, as it unnecessarily complicates the request pipeline for a file that could easily be served by IIS directly.

Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
1

You can place it anywhere.

In IIS you can have a virtualDirectory pointing to that address.

for example,

lets your static files are in "E:/files"

In IIS -- > YourWebSite --> Add an VirtualDirectory ( lets say "content" and points to "E:/files".

Now you can access it from out side like,

http://YourWebSite/content/myimg.png

It will act as if files are placed with in same directory.

Manas
  • 2,534
  • 20
  • 21
0

Edit - more details about App_Data What is the App_Data folder used for in Visual Studio?

You can use

App_Data
I think this is the absolute place to store static content.
Community
  • 1
  • 1
Tasnim Reza
  • 6,058
  • 3
  • 25
  • 30
  • 1
    no its not, app_Data is meant for data - hence its name. from microsoft: "Contains application data files including .mdf database files, XML files, and other data store files. The App_Data folder is used by ASP.NET to store an application's local database, such as the database for maintaining membership and role information. For more information, see Introduction to Membership and Understanding Role Management." – Adam Tuliper Jan 17 '12 at 16:33
  • thanks @adam for your clarification, i know the resource files are stored in App_Data. The question is if you don't want to store your static content in Content folder so where you can put ? my choice App_Data ! – Tasnim Reza Jan 17 '12 at 18:16
  • in any folder outside of the built in folders. app_data has a specific purpose and its definitely not for static content. static content just literally goes into any folder that is mapped through iis. again - not app_data though! Resources files are essentially databases, although theres app_localresources and app_globalresources. – Adam Tuliper Jan 17 '12 at 20:14