4

Every time I add a new class anywhere underneath my App_Code directory in my MVC 3 web application, it gets set with a Build Action of "Content". I need to set it to "Compile" by default. Is there somewhere in Visual Studio to set a default build action for code underneath this directory?

Scott
  • 13,735
  • 20
  • 94
  • 152
  • 1
    Out of curiosity, what code are you placing there? I'm knee deep in a big MVC app right now and have never put anything there, so curious if I am missing out on something. – Matt Greer Jan 03 '11 at 16:52
  • I've got several custom action filter attributes, HTML helpers, etc. Seemed to be the logical place to put them since they're MVC-specific classes. – Scott Jan 03 '11 at 16:54
  • 2
    Schluer I strongly recommend against using App_Code for these kinds of domain classes. It would be cleaner to add either a satellite assembly or an additional folder to your project structure so that these have a specific namespace. In modern ASP.NET development the App_Code directory is rarely the best option. – Nathan Taylor Jan 03 '11 at 17:01

1 Answers1

10

MVC projects aren't Website projects, they are Web Application projects. Website projects (unless pre-compiled) are deployed with source code, and are dynamically compiled by the ASP.NET runtime. Web Application projects are built and deployed as separate stages. App_Code is a special ASP.NET folder which is compiled separately from the rest of the application. This is why the items in App_Code default to Content, so they can be deployed (source) separately of the application (which gets compiled).

In Web Application projects, you're not governed by any standard format for your project layout, its all compiled code at the end of the day. I'd recommend either using the Models folder in the project, or just creating your own, I wouldn't use App_Code.

Matthew Abbott
  • 60,571
  • 9
  • 104
  • 129
  • Thanks for the info. That helps. I'll restructure my project then to avoid use of the App_Code directory at all. – Scott Jan 03 '11 at 17:03
  • 2
    Don't forget if you are moving stuff from App_Code to elsewhere, it doesn't change the build action for you, you'll have to set that to `Compile` yourself. – Matthew Abbott Jan 03 '11 at 17:05
  • But then where do you put the razor helper functions? Scott Gu said that MS cheated and made razor functions live in the App_Code directory because they didn't have time during the dev cycle to put them in a proper location in the Views folder. I am running into this problem because I am using razor helpers and I want to precompile my cshtml code for better performance. Does anybody have a suggestion of what to do with razor helpers? I assume people are just not using them? – Greg Veres Oct 27 '17 at 20:22
  • If somebody comes across this issue, here is how I solved it for us. I kept using the razor helpers and I kept them in App_Code. I left App_Code marked as content so that the build would succeed on VSTS. On our build definition on VSTS, I added the necessary flags to precompile the CSHTML files. And on our deployments I added a Delete Files task that deletes 'App_Code'. I put the Delete Files task above the deploy to Azure. This allows us to use razor helpers and precompile our files and deploy to azure without issue – Greg Veres Oct 28 '17 at 00:41