What is the best place to place static files in asp.net mvc application? - asp.net-mvc-3

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.

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.

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.

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.

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.

Related

Include source code from different directory

I have three different domains all on the same server and I want to run the code on all three domains from one source on the same server, but not sure the best way.
Here's what I have:
domain01.com
domain02.com
domain03.com
domain04.com/sourcecode
I want domain01-03 to run the code inside domain04.com/sourcecode so the user can go to their domain and not have to go to domain04.com to see their site. I want to keep all the code inside domain04.com because I don't want to have to put the code inside each domain every time I make a code change.
For whatever reason I can't get my head around the best way to do this -- and want to do it right.
Any advice?
Thanks!
All you need to do is create a mapping on the first three sites to the appropriate directory in the fourth site, eg map /domain04 to /full/path/to/domain04/sourcecode, then refererence its CFML resources via /domain04 in CFC and include paths. The inference here is the code does need to be accessible via the file system for all sites concerned.
Note that if you also want to server non-CFML files via HTTP (eg: images, css, js), then you will also need a web server virtual directory along the same lines.
None of this requires a framework, it's standard CF / web server functionality.
Are you using a framework? One like ColdBox could make this trivial if your code is written modularly. (Disclaimer, I am affiliated with ColdBox)
If not, it really depends on what the code is. CFCs can be mapped anywhere via ColdFusion mappings. Even .cfm files can be included as long as the file systems are visible. If you're wanting to basically have complete copy of a site in another web root without duplication, I would first consider using a shared source control repo and a build process that checks it out in the appropriate places, and secondly a good old, symlink will also work .

How to access directory outside the application using spring mvc and jsp?

My application is allowed the customer to upload their images. So I am thinking to store those images some where and in my local disk, for instance /home/images/, and save the image path into the database. The problem I have is I am lacking of knowledge how to access the directory outside the application. Thank you in advance.
To save the files you have to use the java.nio.Files API or any other that allows you to create files. To read the images you can use serving static content via Spring MVC. The configuration in XML looks like that:
<mvc:resources mapping="/resources/images/**" location="file:/home/images/"/>
It makes the application look for the files (and folders) in your /home/images/ folder for all requests you make for the /resources/images/**.
Notice the file: prefix. It refers to the absolute path. When you want to set the location of your files relative to the application just leave the prefix out, e.g. /WEB-INF/images/.

how to serve or force file download located from a different directory in meteor to client browser

A user comes to my site and inputs something, and my site generates a file as an output.
Unfortunately i cannot place the generated file on the public directory - as you all now Meteor watches this and restarts every time the public folder content is changed.
so my generated files lives in .meteor/local/build/programs/server/files
so for example i have document.pdf that lives in that directory, I'd like to serve/force/trigger a file download to my client's browser that lets his browser download this document.pdf file.
In general Its not a very good idea to do this. It makes it very hard to scale your app. Node isn't good at serving chunky static files either.
Then also if you have two servers there is a slight chance that the other one's data is requested (e.g if you use a download manager).
I'm not sure but I think Meteor's live code reload doesn't work/is switched off in when in production mode (when using meteor deploy or meteor bundle)
The best thing to do would be to upload your file to S3 and then redirect the user to the file there.
You can also use Iron Router and server side routes to create a dynamic file download.
See Iron Router Server Side docs. Then you set your content type to application/pdf and send back the file directly without saving it to the filesystem. If you need to you can also save it in some other folder and serve it up yourself.
Then have a peek at this answer for an example of reading in and streaming out a file:
Node JS file downloads using a stream.
Since this is a server side route, using express and Iron Router, you shouldn't have to mess with any of the fibers related async issues.

Using a Webform in an MVC3 Area

I am trying to integrate an old ReportViewer Webform into my current MVC3 project. I would like it to be available at http://<server>/Reports/ViewReport.aspx. At first I created a folder in the root of my project titled Reports, dumped the page in there, and it worked just fine.
However, I now have an Area also called Reports, and I had to get rid of the folder in order for the default routing to work correctly.
How can I configure my routing so that the Webform URL appears to be coming from Reports even if it's physically elsewhere in my project?
The easiest way to do this is to use IIS URL Rewrite module. No changes to your application's code or routing. Just place your webpage somewhere in some non-MVC related folder that is also accessible.
http://www.iis.net/download/urlrewrite
But otherwise you could try putting your file directly in area folder as the RouteCollection.RouteExistingFiles is by default false which means that your file should be processed by the usual Asp.net web forms pipeline.
The most important thing is though that you don't put your file inside a folder with configured System.Web.HttpNotFoundHandler handler. By default Views folders have these configured so files within sub-folder tree are inaccessible from request level. Application of course can access them (that's how MVC works anyway).

Access to static files in MVC 3 in an Azure web role on the Visual Studio Development Webserver

I'm having issues serving out static (image) files from an Azure + MVC 3 project when running in the dev web server. I have forms authentication running on the site, and any requests for images are met with a login redirect.
I have been able to make access possible by making my images folder an application (via iis) and explicitly setting my windows user to have access to the images folder, though this only works for a debugging session, and clearly isn't a real solution.
There are a couple of problems it isn't:
Static file requests being picked up by mapped routes
Folder permissions not allowing access to the NETWORK SERVICE account
Rules in web.config requiring authorization for the folder
Currently the images reside in ~/Images/... though I have also had them in ~/Content/... which is where the main css resides. Said css always serves out without any issues.
Notably the images are not served even if you do log in.
I realise that it may be better to store these images in a blob, particularly in the development phase, and for source control, it seems easier to carry these few static resources in a project folder.
EDIT - Question was incorrect. Actually just an issue with some files being encrypted on my hd and others not, causing odd results.
It turns out windows was encrypting all of the files in these directories (not standard behaviour for my configuration, but was happening here for one reason or another). I had a couple of images that weren't encrypted (including the styles.css) but most of the images were. That meant that images wouldn't serve without my credentials against them...
So nothing to do with mvc / azure / cassini!
My guess is that this is some config issue and is not related to Azure itself.
It is probably worth trying similar questions/answers like:
My ASP.NET MVC2 application with Forms Authentication is blocking access even to Images, Styles and Scripts
Also, if you create a new project then do you see the same effect?
If no, then compare the web.config type settings one by one - the difference will be there.
If yes, then it seems like the problem is somehow at the web server/machine level (maybe something to do with anon or windows authentication in web.config and/or app.config)

Resources