In an ASP.NET website with a codebehind at what point are the .cs files compiled?
In an ASP.NET website with a codebehind at what point are the .cs files compiled?
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
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:
- The page directive in your
.aspx
file hasCodeBehind="your.aspx.cs"
- The properties of the
.cs
and.designer.cs
files in solution explorer have abuild-action
ofcompile
.
CodeFile
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.
Key advantage
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:
- The page directive in your
.aspx
file hasCodeFile="your.aspx.cs"
- The properties of the
.cs
file in solution explorer have abuild-action
ofcontent
- The properties of the
.designer.cs
file in solution explorer have abuild-action
ofnone
.
Notes
- Intellisense doesn't like working when pages are set up with CodeFile (you can change to CodeBehind whilst coding and then change back for deployment, though).
- If you change from CodeBehind to CodeFile, then always do a rebuild and re-deploy (and vice versa). This is because when the page was CodeBehind, the
.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.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2015-07-08 Step3 - How to: Host and Run a Basic Windows Communication Foundation Service
2015-07-08 Step2 - How to: Implement a Windows Communication Foundation Service Contract
2015-07-08 Step1 - How to: Define a Windows Communication Foundation Service Contract
2015-07-08 Getting Started Tutorial from msdn
2015-07-08 WCF - Consuming WCF Service
2015-07-08 WCF - Windows Service Hosting
2015-07-08 WCF - WAS Hosting