In an ASP.NET website with a codebehind at what point are the .cs files compiled?

Posted chucklu

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 has CodeBehind="your.aspx.cs"
  • The properties of the .cs and .designer.cs files in solution explorer have a build-action of compile.

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 has CodeFile="your.aspx.cs"
  • The properties of the .cs file in solution explorer have a build-action of content
  • The properties of the .designer.cs file in solution explorer have a build-action of none.

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 .dllin 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.

以上是关于In an ASP.NET website with a codebehind at what point are the .cs files compiled?的主要内容,如果未能解决你的问题,请参考以下文章

Create an ASP.NET Core web app in Visual Studio Code

Paging through lots of data efficiently (and in an Ajax way) with ASP.NET 2.0

Asp.NET Core 在IIS部署 An assembly specified in the application dependencies manifest was not found(示例代

在 ASP.NET Core 中使用 Serilog 使用 Fluentd 将日志写入 Elasticsearch

ASP.NET URL 重写

Asp.NET 没有应用我的 CSS 文件