Code-Behind vs. Inline Code

After religiously adhering to the new, improved code-behind model of ASP.NET for so long, I have to admit it's sort of refreshing to rediscover inline code ASPX pages again. Deploying single web pages to a server without recompiling the entire solution? Making localized edits to single pages that take effect in real time? A single file that contains both code and markup in one convenient self-contained package? Revolutionary!

I've worked with code-behind for so long I actually didn't even know how to convert a page to the code inline model. As expected, it's quite simple:

  1. Open the HTML (.aspx) view of an existing ASPX code-behind page.
  2. Add or modify the following page directive:

    <%@ Page Language="vb" Strict="True" %>
    

  3. Paste all the code from your existing code-behind (.vb) into a HTML script block:

    <HEAD>
    <SCRIPT language="vb" runat="server">
    (page class code goes here)
    </SCRIPT>
    </HEAD>
    

    only paste the code inside the page class, under the "Web Form Designer Generated Code" region. Don't paste the class itself!

  4. Don't forget to add any imports your code needs; these can go just above the page directive.

    <%@ Import Namespace="System.IO" %>
    <%@ Import Namespace="System.Text" %>
    <%@ Import Namespace="System.Text.RegularExpressions" %>
    <%@ Import Namespace="System.Configuration" %>
    

  5. Delete the associated .vb code-behind file.

That's it! You now have a single file webpage that dynamically compiles. Copy that single file to a target website and, assuming it doesn't have any other dependencies, it'll load right up.

Unfortunately, Inline ASPX pages also remind me of some things I didn't miss from the bad old days of ASP programming: spaghetti code, extremely limited intellisense, and crappy debugging. On the whole, I'll stick with code-behind, but I might vacation in Inlineville from time to time.

Inline code pages clearly have their place; I use them mostly for utilities where ease of deployment into existing websites is the overriding concern. However, I do think you can also make a fairly compelling argument that the current ASP.NET code-behind model is a bit too restrictive; you end up with a humongous web.dll that has to be recompiled even if the tiniest bit of code in the most trivial web page in your solution changes. It'll be interesting to see what kind of improved, hybrid behind/inline model we get in VS.NET 2005.

Read more

Stay Gold, America

We are at an unprecedented point in American history, and I'm concerned we may lose sight of the American Dream.

By Jeff Atwood · · Comments

The Great Filter Comes For Us All

With a 13 billion year head start on evolution, why haven't any other forms of life in the universe contacted us by now? (Arrival is a fantastic movie. Watch it, but don't stop there - read the Story of Your Life novella it was based on

By Jeff Atwood · · Comments

I Fight For The Users

If you haven't been able to keep up with my blistering pace of one blog post per year, I don't blame you. There's a lot going on right now. It's a busy time. But let's pause and take a moment

By Jeff Atwood · · Comments

The 2030 Self-Driving Car Bet

It's my honor to announce that John Carmack and I have initiated a friendly bet of $10,000* to the 501(c)(3) charity of the winner’s choice: By January 1st, 2030, completely autonomous self-driving cars meeting SAE J3016 level 5 will be commercially available for passenger

By Jeff Atwood · · Comments