13

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.

Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
BuddyJoe
  • 69,735
  • 114
  • 291
  • 466

4 Answers4

21

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.

davogones
  • 7,321
  • 31
  • 36
  • +1. I also noticed the is a "Convert to Web Application" what is that about? why is it an option in MVC? – BuddyJoe Feb 01 '09 at 05:17
  • Ironically, "Convert to Web Application" is only available for web applications! This option is really for web applications that were converted from web sites. This goes through all of your aspx and ascx files and converts them to web application format. – davogones Feb 01 '09 at 05:23
  • davogones, any thoughts on this one: http://stackoverflow.com/questions/425015/asp-net-wcf-and-could-not-load-file-or-assembly-appwebhamznvwf I have been wrapped up on a few other things and haven't been able to call Microsoft yet – BuddyJoe Feb 02 '09 at 14:28
10

Generally, I place "service layer" and "utility" classes in a separate project and add it as a reference to my web application. With the MVC framework, I don't really see a need to compile classes at runtime.

David Brown
  • 35,411
  • 11
  • 83
  • 132
  • What about for development speed? Change code/save/refresh browser versus Change code/save/build.../refresh browser. – Brian White Apr 30 '12 at 15:33
  • David, Do you put your projects in the same Solution and put the reference or completely different Projects and grab the Reference? – johnny Mar 28 '17 at 15:33
  • 1
    @johnny In the same solution, unless it's a general-purpose library that I re-use elsewhere. – David Brown Mar 28 '17 at 15:57
10

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.

King Friday
  • 25,132
  • 12
  • 90
  • 84
  • 1
    I've stopped working with MVC by the way. I just use pure Matrix-style with URL Rewrite. There is a ton less code. Well, I'm starting to go all Linuxy now anyway while we are at it. Node.js is really killing it. – King Friday Feb 13 '12 at 03:47
  • Also helps so you can test [RequireHttps] and leave that alone without a bunch of junk statements in code. – Damon Drake Oct 25 '12 at 19:30
  • This solution is a black terror of the night that feasts on the souls of little orphans. Please don't justify your terrible release management alongside this answer. – John Zabroski Aug 23 '13 at 03:23
  • 1
    "Release Management" :) that is when you are on a team of developers in a large project, not when you have 1000 smaller ecommerce customers needing a quick fix where each customer has slightly different requirements than the next. That would kill my business but I feel you for your scenario. – King Friday Aug 23 '13 at 04:09
  • I am struggling with this right now. Everything looks good on a small application, but when things get complex maintaining everything with VS is becoming a heartache. – johnny Mar 28 '17 at 13:44
  • I completely jumped ship on asp.net a long time ago to node and go but aside from that you should use VS the way it was intended for larger applications and ignore this. My use case was quick fixes for small to mid clients. – King Friday Mar 28 '17 at 13:50
0

For those who don’t want a second project/class library and just want a simple folder for a few c# classes, I propose “App_Classes”.

RitchieD
  • 1,831
  • 22
  • 21