What files should go in the ASP.NET MVC Content folder? - asp.net-mvc

I see that ASP.NET MVC generates a Content folder by default, which stores the site.css file. I have a site that uses some image files and XML files in order to do its job, yet I do not want to have those files as embedded resources. Should I just put the files in the solution within the content folder? Should I turn off the "Do not copy" build option on each resource since I will need to deploy the files to the website? Or should these files not even live in the solution and be loaded from the file system?

It's a good approach to add all your files/resources to the solution (at least you can see them in VS ;-) Other developers won't be surprised that your solution really needs some other files.
If those files are the 'content' of your website (like css files) then you can store them in the Content folder. It is up to you what folder structure you come up with. For example you might want to store your images in folder related to the modules of your application, or just all in the images folder. MVC doesn't enforce any particular way of keeping your resource files.
Just leave the Build Action set to Content and Copy to Output Directory to 'Do not copy'.
You might want to take a look at T4MVC project (also available as NuGet package) which will help you in keeping urls to your images / files compile-time safe.

Related

How to serve some static files of a certain file type, but not others?

My ASP.NET MVC project's root directory contains some typical static files, like robots.txt, manifest.json, browserconfig.xml, etc. If I'm not mistaken, each of these examples I listed should be able to be served with no involvement from MVC via GET requests to the root directory (i.e. mysite.com/manifest.json — if that's not true, please let me know).
I know from this answer that I can configure this behavior per file type in the Web.config. My question is, what if there are other .json files in my root directory that I don't want to serve, like compilerconfig.json or bundleconfig.json (both files generated by IDE tools)? What's the best way for the application to be able to serve some files of type X, but not others?
You can always ignore them via routes:
routes.IgnoreRoute("{somefilename}.json");
Another alternative would be to move the files you don't want to be served to another folder and add a web.config file to it to manage what gets served (or doesn't).
I am sure there are other ways. Modules come to mind...

MVC File Structure Standards

I am just new to MVC, we are building a massive system and have alot of namespacing in our site.
Where is the standard place to store files (CSS, Images, .JS) etc?
Would it be good to put them under the Content folder in sub-folders to their namespace or drop them in with their respective parent files or both.
The default project structure includes a Content folder for CSS files and a Scripts folder for JavaScript files. A lot of people use this existing template, especially since a lot of Nuget projects may rely on this.
I personally like to put all of the content in a Content folder, and have a subfolder under this named Css and Scripts. It's really a matter of preference though. Do whatever is consistent and well-organized. That will be the key to making the application more easily maintainable.
Its better to put it in saparate folder as Script(js file) at root level.
Add sub folder in content folder for Images and Css etc.
we can also create multiple controller and views for each section of your project.
like for login section you can add Authentication Controller.
we can also use Helper class for adding general function and use it in every where.
you can see following link
http://msdn.microsoft.com/en-us/library/dd410120(v=vs.100).aspx

Prevent IIS7/ASP.NET locking directory with images

I have an ASP.NET webapp using the ASP.NET MVC 2 framework. It allows users to upload files to an uploads folder. The issue occurs when an image within a sub-folder is accessed by a web browser:
http://mywebapp/uploads/image_gallery/sub_folder/image.jpg
The uploads folder is static and can't be modified by users, but anything below it is intended to be modifiable.
In the above example, the image_gallery folder becomes locked because w3wp.exe appears to create a handle on the sub_folder directory (using process explorer by sysinternals). I am still able to rename the sub_folder directory and the handle seems to stay with it after a rename, but i can't rename the parent folder (image_gallery in this case).
I can still browse within the folder and view other images and files etc. But can't rename the parent folder.
As this is using the MVC 2 framework i've put in an exclusion for the uploads folder like so:
routes.IgnoreRoute("upload/{*pathInfo}");
into global.asax, so i'm assuming that ASP.NET is serving up those images directly (outside of the MVC framework)
So I guess my question is, is there any way to prevent IIS from putting a handle on specific directories or forcing it to remove a handle? Is the MVC 2 framework doing something tricky even though i have the ignoreroute specified?
Thanks in advance for any tips!
I had the same problem, and after much investigating I've found the culprit:
Web.config
<configuration>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
Setting this option (which makes all http modules run on ALL files, static ones included) to false fixed it for me.
I believe, directory handle gets created in worker processor because ASP.NET typically watches the file system for changes - this allows it to respond to change/addition of config files, new dlls etc.
I suggest that you move from the concept that users can control physical directory structure on the web server. Typically, what user can create is an logical directory structure but physical structure would be controlled by your program logic. Your logic will store the logical structure created by user and its mapping with actual physical structure on the web server.

Include MVC views and master pages as DLL resources instead of separate files

Does there exist a method when publishing an ASP.NET MVC application to completely remove the .aspx view files (and if possible .master too) by compiling them into the application DLL as resources?
The published application would just be the /bin folder, Global.asax and web.config, a default.aspx if needed, and whatever is in the /Content folder. All the views would be contained in the MyProject.dll file.
To clarify I don't mean where every .aspx is overwritten with a 1 line dummy file referencing the correct resource, but where those files can be eliminated entirely in the published app.
The goal here is to simply change management and all the auditing and layers of people surrounding it - one file gets deployed, that file replaces the existing file, and no messing around with a stack of .aspx files that have to be added/removed/updated (yes, SVN or a similar solution handle that problem instead, but management politics prevent this).
Is this what you are looking for?
It's possible with the web forms view engine but you'll have to extend the path provider yourself.
Here is a question here at SO about the same thing:
Using VirtualPathProvider to load ASP.NET MVC views from DLLs
If you use the Spark view engine, it already has additional path providers built in.
The documentation can be found here:
Adding a view folder to config
It allows you to locate your views inside a DLL as an embedded resource, somewhere else on the file system, using the default virtual directories, or plug in your own custom provider.

Why not have the Scripts folder inside of the Content folder for ASP.NET MVC project?

Out curiosity I was wondering if there was a logical reason to have the Scripts folder not a sub folder of the Contents folder in an ASP.NET MVC project. The Contents folder typically contains your style sheets and images and for some reason it would seem natural to me to also include the Scripts folder in there as well.
Possibly because scripts could be denoted as providing more functionality than style and design items, so it could be considered a portion of your business logic.
The scripts and content folders are containers for client-side consumed files. They don't affect your MVC applications in any way. You can rename and move them around as you see fit, provided you update the URL references to them to point to the new path.
Having said that, I personally tend to rename these to js and css mostly because this makes my URLs shorter and easier to read and understand.

Resources