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

3

16

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.

Volney answered 15/1, 2014 at 10:13 Comment(6)
Is this a Web Site project (File->New Web Site), or a Web Application Project (File->New Project)? It makes a huge difference. Is there a .csproj file?Branson
I cannot see .csproj file in the directory. Based on the @Page tag, I had assumed it was a website: <%@ Page Language="C#" MasterPageFile="[MASTER PAGE PATH]" AutoEventWireup="true" Inherits="[CLASS NAME]" Title="[PAGE TITLE]" Codebehind="[CS FILE]" %>Volney
As you can see from theyetiman's post, you are not in luck unfortunately. The fact that it says "Codebehind" in your Page attribute shows that it's a web application, not a web site. You need to recompile the site.Doubleness
@Doubleness in actual fact, it doesn't matter. You can use CodeFile in a web application as well as a web site. I do it all the time.Nonstandard
@Nonstandard Yeah, I've seen examples of that myself so I know it works, but is it officially supported? When I took a company course in .NET web development, the teacher did not know of this possiblity (and this was a course supported by microsoft for certificates etc).Doubleness
@Doubleness I've no idea if it's officially supported by Microsoft, but it certainly works and I've been using it in production for years (currently on .NET 4). I suspect it will continue to work so long as WebSite and CodeFile concepts are supported by Microsoft (probably for a long time)Nonstandard
N
36

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 .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.
Nonstandard answered 15/1, 2014 at 10:22 Comment(5)
(In response to "Is putting CodeBehind="my.aspx.cs" enough to compile it to the .dll?") ... NO! That just tells the ASP.NET runtime that it should be compiled into the .dll, but if it isn't you'll get an error. You need to have both the page directive and the .cs (and .designer.cs) build action set to compileNonstandard
how can i get properties of *.cs? i don't see that option and if i click on it ans switch to properties window i see FullName and FullPathAtavistic
@ebramtharwat you may be developing a web site as opposed to a web application. In this instance, I think everything is CodeFile by default and you can't change it. I might be wrong, though. In a web application you can right-click > properties to see the build action.Nonstandard
i have a WebSite and it's CodeBehindAtavistic
@ebramtharwat sorry but I don't know how to help. I just checked and if you create a new Web Site in VS, it creates CodeFile pages. I personally never use Web Sites, only Web Applications so I'm not sure of any caveats.Nonstandard
C
0

For the setup I use, the .cs files are compiled when building the project. This means it is the .dlls in the bin that need to change, not the .cs files directly.

The .aspx files can change at any time, but I think you need to rebuild the project in order for the code behind to take effect.

I have replaced singular .dlls before without any problem (though it's not good practice).

Cardcarrying answered 15/1, 2014 at 10:17 Comment(0)
W
0

Apparently what you have done should work. Check if Cacheing has been implemented. Otherwise publish the code and deploy the dll, instead of .cs file. I would recommend to test in staging server before you go live.

Weeping answered 15/1, 2014 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.