Minimizing code behind in ASP.NET 2.0

Onion Blog

Syndication

As a general rule, I like to keep my applications minimal, so that every file and every piece of code has a place and a purpose in my mind. Whenever I created a web project in VS.NET 2003 the first thing I would do was delete the global.asax and its code behind, delete the AssemblyInfo.cs file (since it rarely makes sense to version your code behind assembly), open the web.config file and reduce it to a single self-terminating element <configuration />, and finally rename the generated WebForm1.aspx file to Default.aspx, fixing up the corresponding code behind to match.
 
That's why I often tell people that one of my favorite features of VS.NET 2005 is the "Empty Web Site" template, which generates nothing but a directory for you, leaving a lovely white canvas on which to paint your application. Along these lines, whenever I add a new WebForm to a site, I typically leave the "Place code in separate file" checkbox unchecked, since I find more often than not that my pages don't require any supplemental code at all - most of my code ends up in a data access layer or set of utility classes that I reference declaratively through object data sources or in data binding expressions.
 
On the occasions where I do find I need supplemental code for a page, however, I do like the separation that code behind provides and generally prefer that over embedding code directly in a runat="server" script block in the .aspx file itself. Unfortunately, what this means is that I must retrofit the page to use code behind with the following steps (assuming the page in question is named MyPage.aspx):
  1. Add a new class to the site (File | New File, choose Class template, Name=MyPage.cs, be sure to respond 'No' when prompted to place the file in the 'App_Code' folder.
  2. Modify generated class to be partial and to inherit from Page.
    public partial class MyPage : Page
    {
    ...
  3. Modify the @Page directive in MyPage.aspx to reference the new code behind file.
    <%@ Page Language="C#" Inherits="MyPage" CodeFile="MyPage.cs" %>
  4. Remove the <script runat="server"> </script> block from MyPage.aspx.
 
What I'd really like to be able to do is right-click on a .aspx file with no associated code behind file in my Solution Explorer and say 'Add code behind'. Plugin anyone?
 

Limited training time? Need to learn ASP.NET 2.0 and SQL Server 2005?
Come join Fritz Onion and Dan Sullivan the week of July 17, 2006 in Waltham, MA for a
Pluralsight Double Feature: ASP.NET 2.0 and SQL Server 2005
Register today!

 

Posted Mar 21 2006, 07:56 AM by fritz-onion
Filed under:

Comments

Carl Lambrecht wrote re: Minimizing code behind in ASP.NET 2.0
on 03-21-2006 11:41 AM
You specifically indicate that when prompted to place the file in the App_Code directory, be sure to respond 'No'. Why? Although on the one hand I can see the preference to keeping aspx and codebehind files together in the development environment, wouldn't placing the codebehind in the App_Code directory simplify deployment?

Cheers,
Carl
Fritz Onion wrote re: Minimizing code behind in ASP.NET 2.0
on 03-21-2006 12:23 PM
You actually can't place the code behind file in the App_Code directory if you are using the CodeFile attribute and partial classes (the default model in 2.0). It must be compiled with the .aspx file and files in the App_Code directory are compiled into a different assembly.
TheChaseMan's Frenetic SoapBox wrote Code-behind
on 03-28-2006 6:29 PM
Elaine Krause wrote re: Minimizing code behind in ASP.NET 2.0
on 04-03-2006 8:11 AM
In VS 2003 there is a much easier way to add a code behind file to an aspx page. Just exclude the .aspx file from the solution and then add it back in. A dialog asks if you want to add a code-behind file and you just say yes. -Elaine
darenhan wrote re: Minimizing code behind in ASP.NET 2.0
on 04-05-2006 10:53 PM
hi there,

can u post the slides / code for provider model? I like to know how can i do personlisation for a portal. Thank u very much.

I need the help. Hope u can render me. Someday I will buy u coffee one day.

-Daren
Garry Labana wrote re: Minimizing code behind in ASP.NET 2.0
on 04-18-2006 2:00 AM
Hi;

Following on from this point, I am receiving an error when i put my code behind in the App_code folder:

Error 106 The file '/MyApp/Logon.aspx.cs' is in the special directory 'App_Code', which is not allowed.

This actually confirms what Fritz says.

HOWEVER when reading the an article on MSDN about the ASP.NET 2.0 internals, (section Compilation in ASP.NET 2.0)

http://msdn.microsoft.com/asp.net/default.aspx?pull=/library/en-us/dnvs05/html/Internals.asp

it states that codebehind source code files can be put into the App_Code folder. This is slightly confusing as this is a contridiction to what is being said in this post. Can someone clarify what is the correct process?
Jon Galloway wrote re: Minimizing code behind in ASP.NET 2.0
on 11-30-2006 4:36 PM
I wrote a simple macro to do this. No plugin yet, though.

http://weblogs.asp.net/jgalloway/archive/2006/11/30/macro-to-add-a-codebehind-file-to-an-aspx-page-in-vs2005.aspx

Add a Comment

(required)  
(optional)
(required)  
Remember Me?