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?
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.
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.
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.
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?
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.
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
if you create a class, it will ask you if you want to add it on a App code folder.
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.
Related
When I use Vs 2012 Express edition to create a website , I remember I should place the class file *.cs in the system folder \App_Code, and Vs 2012 can compile these files automatically. It's OK.
When I use Vs 2019 (Microsoft Visual Studio Community 2019), I find the the class file *.cs in the system folder \App_Code can't be compiled automatically, I have to set it by myself, you can see Image A.
But if I place the class file *.cs in in root folder of a project , they can be compiled automatically, it seems that Vs 2019 set Compile option as default, you can see Image B.
Is is a bad way to place the class file *.cs in the system folder \App_Code when I use Vs 2019 ?
Image A
Image B
It is not good or bad to place a class file *.cs in the system folder App_Code when you use VS2019 or some other VS, it depends on your goal for those files.
TLDR: put non user control sources in App_Code if you need server-side behavior (on the fly updates), but don't mark its build action as compile but rather content, and therefore forget about benefiting from IntelliSense.
For a website project, you have the special folders Bin and App_Code, for code shared between pages. Files inside App_Code do not have to be source code, it can be a content file. Files inside are handled by asp.net in special ways (1).
Source code inside App_code and subfolders are automatically compiled at runtime. When the website is running, you can update those source code, and they will be recompiled on the fly, just like Live unit testing in VS (1). This will happen when the website is running on the server too (2).
The App_Code folder and its special status in an ASP.NET Web
application makes it possible to create custom classes and other
source-code-only files and use them in your Web application without
having to compile them independently. (1)
But
User controls are not allowed in the App_Code folder. This includes
both single-file user controls and user controls that use the
code-behind model. Putting a user control .in the App_Code directory
causes the user control's code to be compiled out of its required
sequence and therefore is not allowed. Note that user controls do not
need to be in the App_Code folder; they are already available to pages
anywhere in the application. (1)
I add that you should not add source code in App_Code and leave it marked compile as build action, because then it will be both compiled by VS and asp.net, leading to duplicate references. Instead, leave it as content, but then IntelliSense won't see them as source code. (2)
Vishal R Joshi (2) gives the piece of advice:
You can add code files under any folder call it “CodeFolder”,
“Controllers” or anything that makes sense in your project… Just avoid
putting them under “App_Code” unless you specifically want the server
side compilation behavior… (2)
References:
Shared Code Folders in ASP.NET Web Sites
App_Code folder doesn’t work with Web Application Projects (WAPs)
I get some troubles with a DLL reference.
I have MySql.Data.dll in my bin folder. And I added the reference of this dll to my project.
I can well use classes from this dll (like MySqlConnection, MySqlClient, etc) in aspx.cs but i cannot use it in classes from files from App_Code folder !
For all files situated in App_Code folder (my model classes) , the using MySql.Data; doesn't work. While it works in aspx.cs files.
As you know, there are two types of projects that you can use to create a website in Visual Studio. The App_Code folder works for Web Site projects, but since you stated that you're using a Web Application project, this is causing you problems.
Here's some additional reading on this topic.
In short, I recommend just creating a different (non-built-in) folder to put your random code files in. Name it something like "code", "classes", etc.
at vs2008 i could set App_Code folder, but at vs2010 i can not do it, that's why i put my dataset's and class' to App_Data folder. Either I do not know even App_Data folder is secure? Any suggestions?
Thanks
Remember, that Web Site Project can contain App_Code folder and Web Application Project can't! Because all project is for code.
If you want to add the App_Code folder, you have to manually add the folder.
Add->Add New folder--> you have to give it the folder name "App_Code"
when you do it, and vs2010 automatically will change its folder type.
good luck
If you want to use standalone class files (in "App_Code" dir) in VS 2010 project, be sure set the File Properties > Build Action for each *.cs or *.vb file to "Compile" in the Properties > Advanced dialog.
Also, use the same namespace as your project and these standalone classes will be readily available to the rest of your project.
Took me a bit to realize the default setting in my VS was "Content" instead of "Compile."
Have you tried right-clicking on the project in the Solution Explorer, selected 'Add...' and looked under ASP.NET folders?
From memory, even previous versions of VS don't automatically put the App_Code folder in your project by default.
If you just create a folder called App_Code it will recognise it as the App_Code you want, it is strange that in the add .net folder it doesnt appear.
This the route I took for it, and it all works fine.
Workaround:
If we are using App_Code in a web application project, we need to
rename into any other folder name.
http://blogs.msdn.com/b/chiranth/archive/2014/02/19/visual-studio-2012-unexpected-behavior-crash-and-irrelevant-errors.aspx
I hope this will help you - the .cs files should not be placed in a folder. Just put them in the project, alone, and not in a folder. Then your classes will be visible. This is a difference in VS 2010.
I am looking for some advice on how what is the best approach to subweb folders and having mutliple bin folders in the WebSite Project model. For adding new pages at a later stage without recompiling the core files of a website and without building a full fledged Plug-in framework api. I am aware of being able to drop in the compiled dlls into the main bin folder and to just copy over the new page files to a sub folder but I am looking for a more organized file/folder approach.
Here is the how it was done with WAP:
Moving the Code-Behind Assemblies/DLLs to a different folder than /BIN with ASP.NET 1.1
Multiple /bin folders in ASP.NET
I should also mention that I see that I can still do it the old way with the website project model by making the adjustment to the config section mentioned here but I was wondering if that has any side affects.
AssemblyBinding in Web Config and XMLNS
Are you trying to copy new DLL's or new sources to your site? Note that for sources, the best place in web sites is the App_Code folder. In there, you can create any arbitrary folder structure that you like, and it will all be built at runtime into an assembly. Then, every page gets a reference to that assembly and is able to use types from there.
I've got a ASP.NET project without the .proj file. How do I recreate the project file, so I can work on it on my machine?
Is this possbile?
Are you sure it's not a Web Site Project, as opposed to a Web Application Project? Web Site projects do not have project files - you open a folder rather than a project file. See here for the differences between the two.
To check, each page of a Web Application project will have a .aspx, a .aspx.cs and a .aspx.designer.cs, whereas the Web Site project pages don't have the designer files.
You can create a new project and then you can use the "Add Existing Item" and add each of your files (you can select multiple files).
EDIT:
Or your can drag and drop the files into the "Solution Explorer"
First of all, you need to run VS2005 SP1 or later. In VS2005, they removed the project file. They reintroduced it again in SP1.
If you don't have a project file, your project is called a "Web Site". If you have a project file, it's a "Web Application". One difference between the two is also that in a web application, you compile all code-behind files to a single .dll. In a web site, each code behind file goes in its own dll (or are compiled dynamically at runtime)
I believe that if you right click the project in solution explorer, there is a "Convert To Web Application" menu item.
Note, you must rename the App_Code folder to something else, otherwise it will be compiled both at compile time, and at runtime.
Personally I much prefer web applications to web sites