In Brief: In an ASP.net website with a code-behind, at what point are the *.cs files compiled?
Context: A colleague who has since left, deployed a website with a .cs code-behind to a shared server. I have made a small change to a .cs file, which I should expect to reflect on one of the pages but it has not yet appeared. I have restarted the application pool, however I am loathe to reset IIS on the server as there are couple of other teams' apps which might be be in use on the same server.
cs file is located in the App_Code folder, which is a special ASP.NET folder used in Web Site Projects for class files.
compiled files in the BIN folder. The . compiled file is a marker file for each page and control in the Web site, and identifies the class used inside of the assembly. These files are not optional as they map the ASPX pages to the appropriate precompiled classes in the precompiled assemblies.
Code-behind refers to code for your ASP.NET page that is contained within a separate class file. This allows a clean separation of your HTML from your presentation logic. The following sample illustrates an ASP.NET code-behind page: MyCodebehind.aspx.
In a dynamically compiled Web site project, ASP.NET compiles the code in the App_Code folder on the initial request to your application. Items in this folder are then recompiled when any changes are detected.
This applies to Web Application
projects as opposed to Web Site
projects, which are CodeFile
by default, and don't allow changing the build action...
In ASP.NET Web Applications
you have two methods of deploying your pages; CodeFile
and CodeBehind
. By default pages will always use CodeBehind
but you can change this.
CodeBehind compiles your .cs file into the .dll file in your bin
folder at compile/build time, and then you deploy that to your web server. There is no need to deploy the .cs file to your web server. If you do, it will just sit there being unused.
To configure a page with CodeBehind, ensure that:
.aspx
file has CodeBehind="your.aspx.cs"
.cs
and .designer.cs
files in solution explorer have a build-action
of compile
.This causes ASP.NET to compile the .cs file on-the-fly on the server. This means that your .cs
file needs to be deployed to the web server. It also means that your .cs
file will not be compiled at compile/build time and therefore not built into your .dll
in the bin
folder.
With CodeFile, You can make changes to the .cs
file and deploy just that file to see the changes on your production web server. No need to re-deploy. No need to recycle the app pool. This can be very useful in a lot of situations.
To configure a page with CodeFile, ensure that all of the following are met:
.aspx
file has CodeFile="your.aspx.cs"
.cs
file in solution explorer have a build-action
of content
.designer.cs
file in solution explorer have a build-action
of none
..cs
was compiled into the .dll
in the bin folder, and will
remain there when you change to CodeFile. The CodeFile will be
compiled on-the-fly and you will get the same code/classes defined in
the .dll
and in the on-the-fly compiled code, which will lead to
runtime errors.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With