Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Project and the App_Code folder

How come App_Code is not a choice in the Add ASP.NET Folder submenu in the VS solution explorer? I realize you can create one yourself manually by just renaming a New Folder, but what is the rational here? Is this not where you are supposed to put "utility" or "service layer" type classes?

On a MVC project side note. I do like the fact that there is a reference to System.Configuration out-of-the-box unlike the default ASP.NET Web Form Projects.

like image 447
BuddyJoe Avatar asked Feb 01 '09 05:02

BuddyJoe


2 Answers

This is probably because an ASP.NET MVC project is a web application rather than a web site. In a web site, class files are compiled dynamically at run-time and must live in the App_Code folder. In a web application, everything is compiled statically and class files can live anywhere in your web application.

As David Brown pointed out, it's generally recommended to put extra class definition in a separate class library and then reference that from your web application. If you write unit tests or reference your classes from config files, it can be challenging or impossible to access these classes if they are defined only within your web application.

like image 129
davogones Avatar answered Sep 17 '22 11:09

davogones


The real answer is because Microsoft wants you to buy in that you need VS to do MVC. However, to convert the project to pure JIT which is way easier to work with...

You can do it pretty straight forward.

(1) Move all folders containing class files to App_Code (leave Views folder where it is)
(2) Make Global.asax not needing a code behind or inherits reference by removing those attributes then placing

[script runat="server"] 
... place contents of code behind's class inner code 
[/script] 

and delete the code behind for good

(3) In Bin Remove the project DLL reference making sure to keep the other necessary DLLs in terms of running MVC
(4) Remove solution files and properties folder and obj folder

I do this routinely for nop MVC as its much simpler to work with the web tier on JIT. Developers that don't understand this are not working in the trenches every day on many websites with various custom quick fixes needed. The sites we work on get hundreds thousands of hits per day so using JIT has no performance penalty after the initial bump.
Seems less is more when it comes to smaller website development.

like image 44
King Friday Avatar answered Sep 17 '22 11:09

King Friday