40

I was trying to create a Dynamic Data Website using VS 2010 RC. An attempt to create an App_Code folder where I would put a LINQ to SQL class, failed. When I selected 'Add ASP.NET Folder' to add the folder, I had options to create only the following folders:

  • App_GlobalResources
  • App_LocalResources
  • App_Data
  • App_Browsers
  • Theme

What happened to the App_Code folder?

Vadim
  • 19,585
  • 14
  • 61
  • 100

8 Answers8

35

Why not create another project, a class library, and put it in there. That's by far the best way to keep those classes separate from the rest of your website.

To answer your specific question about the App_Code folder, it's available in WebSite projects as an Asp.Net folder you can choose from. But from a web application project, you can just create a new folder and name it App_Code and it will work just fine. This is as of Visual Studio 2010 RC.

Chris Conway
  • 15,457
  • 22
  • 91
  • 111
  • It's a demo application. I want it to be as simple as possible. – Vadim Feb 17 '10 at 01:41
  • 1
    I would still add another project for your dal classes. It really doesn't add any complexity and you can use it again if you need to make it more production level. – Chris Conway Feb 17 '10 at 01:48
  • 1
    I agree with the suggestion, but this really doesn't answer the question. – Samuel Neff Feb 17 '10 at 02:24
  • @Sam: Chris answered the question in the second paragraph of his answer. In "web application" projects (like "Dynamic Data") you have to create "App_Code" manually as a normal folder, that's all. That's not specific to VS2010 but also the case in VS2008. – Slauma Feb 19 '10 at 17:02
  • @Slauma, the second paragraph wasn't present when I made my comment. – Samuel Neff Feb 19 '10 at 17:58
  • I could have get a clue looking at the time of your post and of Chris' edit. Sorry! – Slauma Feb 19 '10 at 19:37
23

you can't/shouldn't add the App_Code folder to a Web Application because this folder is designed for runtime compilation by asp.net and the Web Application project is designed for you to precompile your website into a dll which you can find in the Bin folder and deploy to your site. If you do add this manually, you may end up getting name clashes as the class will be precompiled to the dll and then asp.net will again try to recompile at runtime.

So, for Web App Projects, you should instead Add a folder called something like 'CodeFolder' and then add you class in there. Then right click properties on that class file and set its build config to compile rather than content.

I am working on my own web project for www.athenatennis.com.sg and am going thru a similar learning curve with that.

letapz
  • 229
  • 2
  • 2
  • 8
    "you can't/shouldn't add the App_Code folder to a Web Application because this folder is designed for runtime compilation" I think the accepted answer should be edited to include this. It explains **why** the original question is a bad approach, not just suggesting a best-practice. @Chris Conway implies that there is no problem with creating an App_Code folder, but I have been personally burned by this in several ways. – CrazyPyro Feb 15 '12 at 18:32
3

You can manually create the App_Code folder and add your class inside. One thing you need to take note is... in the file properties, make sure the Build Action is set to Compile.

CK Lam
  • 31
  • 1
  • This is super-important! By default, it doesn't appear to set this for class files outside the App_Code folder, in .NET 4.0+ web applications (not sure about lower versions). This caused all my IHttpModules to not compile and fire off when testing, until they were set to compile. – Jacob Apr 02 '14 at 14:00
2

I also found this funny that it is missing. The thing is you "don't" need to create the App_Code folder you just add the classes to any new folder.

In VS 2005 any class outside the App_Code would give a compile error.

So I also want to know why they decided to remove this constraint? Was it because of people complained about it, or did the design concept changed and it was deemed necessary?

Bernard
  • 21
  • 1
1

If you want app_code folder, you must create "ASP.NET web site". Once done, right click on website (Solution Explorer)-> Add ASP.NET folder->App_Code.

If you are looking at app_code folder inside "ASP.NET web application", you are wasting time.

Anuj Balan
  • 7,205
  • 23
  • 52
  • 89
Djay
  • 21
  • 1
0

if you create a class, it will ask you if you want to add it on a App code folder.

fujiko
  • 1
0

It is very easy just add the new class from Add New Item Visual Studio 2010 will automatically show the message that "Do You want to place the file in App_Code Folder".Just click ok.![enter image description here][1]

saqib amin
  • 25
  • 3
0

in VS 2010, you need to create a solution, then right-click -> add new website, then when you add classes, linq2sql etc, vs will create the app_code folder for you

cvanniekerk
  • 139
  • 1
  • 1
  • 7