18

In ASP.NET Webform, App_Code is the standard folder for putting code and using it at run-time. But I think this folder is kind of different in ASP.NET MVC, my question is:

  • where should I put my code ( Extension methods, Helpers, ... ) in ASP.NET MVC. When I store code in App_Code folder, I can't use them in controller but they work fine in views.

  • About Entity Framework, the same question, where should I put edmx and tt files? I'm not using Code-First

Update:

After some search, finally I created a new Class Library project in my solution, code is available in all controllers and views. I still don't know why the code in App_Code is not available in the controller

Anup Shah
  • 167
  • 1
  • 13
Mironline
  • 2,755
  • 7
  • 35
  • 61
  • 2
    To use app_code classes in controller check here http://stackoverflow.com/questions/1222281/app-code-classes-not-accessable-asp-net – yayadavid Apr 03 '13 at 14:35
  • 1
    App_Code classes are compiled during app initilization, so, in compile time they are not available. – Jone Polvora Feb 17 '15 at 23:25
  • App_Code folder will prevent you publishing precompiled MVC project (to prevent first page hit waiting) and will throw an error `"The directory '//App_Code/' is not allowed because the application is precompiled"` – Muflix Sep 21 '16 at 09:26

6 Answers6

37

I had the same issue, my class Utility.cs was not recognized inside my MVC project. I changed the Build Action "Content" to "Compile" and that solved my problem. Hope that helps.

polykos
  • 386
  • 3
  • 3
  • 2
    if I do that in my project I get: "The call is ambiguous between the following methods or properties" – developer82 Jun 07 '14 at 19:04
  • 3
    This defeats the purpose of App_Code. App_Code exists for you to be able to drop the code files in there and have them dynamically compile at runtime. Without even having them a part of project yet alone set Compile Build action on them. – mare Mar 27 '15 at 07:57
12

App_Code is necessary in Web Site projects because it has a special meaning. It means "Don't serve these files to a web browser". In ASP.NET MVC, files are not directly served to the browser in most cases, so App_Code is not necessary. You can place code files whereever you want, in any folder you want because these files are compiled into a DLL and not typically published to the website itself.

Using a stand-alone library is also a fine solution.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • I have the same question. Where should I put my code which is not included in DLL? There is still some code remaining in web application. OK, lets assume I can put the code files whereever I want. Is there some standard place where web developers expect the code to be? App_Code folder? Helpers folder? I have notice in some examples from MS, there are folders like Helpers and Models. – Tomas Kubes Jan 12 '15 at 09:48
  • @qub1n - Why would you have code that is not in a DLL? Even code in your code-behinds gets compiled into a DLL – Erik Funkenbusch Jan 12 '15 at 14:37
  • I could. So is the best practice to have really no single file with code in Web Application except Global.asax.cs? – Tomas Kubes Jan 13 '15 at 06:50
  • @qub1n - I really don't know what you are talking about.. of course you can have code in your web application, they just get compiled into the applications DLL. They do not need to be in App_Code. – Erik Funkenbusch Jan 13 '15 at 15:36
  • @ErikFunkenbusch You don't really explain why App_Code exists in the first place. It's the place where you put code files to get dynamic compilation (at runtime). That's the number one reason for App_Code and why it is special. You can put code in any folder you want but you will have to mark it with Compile action to compile while in App_Code that is not necessary, in fact, most files in App_Code aren't even a part of a project. – mare Mar 27 '15 at 07:56
3

No one really explains why App_Code exists in the first place. It's the place where you put code files to get dynamic compilation (at runtime). That's the number one reason for App_Code and why it is special. You can put code in any folder you want but you will have to mark it with Compile action to compile while in App_Code that is not necessary, in fact, most files in App_Code aren't even a part of a project.

It does work in ASP.NET MVC projects too you just have to name your code files with .cshtml extension.

mare
  • 13,033
  • 24
  • 102
  • 191
2

I'd recommend starting with this tutorial. It uses EF code first, but you can simply replace the code first DbContext with a normal edmx ObjectContext if you wish (hint: Models folder).

App_Code is specific to Web Site Projects. IMO, its a terrible way to organize all but the simplest of web sites. An MVC project is a Web Application Project, so there is no App_Code and your classes can be defined pretty much anywhere.

jrummell
  • 42,637
  • 17
  • 112
  • 171
0

Well, try to download some already existing opensource projects. One of them is rather complex, but very cool if you'll get understanding of it - orchard

The best practice is moving this logic to DAL (data access layer) or BLL(business logics layers). Depends on complexity of your application.

Johnny_D
  • 4,592
  • 3
  • 33
  • 63
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
  • Please tell me more. This would make a terrific answer if you wrote an introduction to `App_Classes` and instructions on how to use it. – WonderWorker Jun 23 '17 at 15:16