Asp.Net inline (single-file) vs code-behind

asp.net

In the transition from classic ASP to Asp.Net some developers took to putting their server side code in a block at the top of their HTML ala:

<%@ Import Namespace="MyDll" %>

<script runat="server">
    void Page_Load()
    {
    }
</script>

This single-page model has some advantages as Jeff Atwood describes, however, in my experience I have seen nearly all code put in a separate code-behind file in recent times (ie with VS 2008).

Nevertheless, it turns out a colleague strongly prefers the single file (inline) method over the separate code-behind method.

What are the strengths and weaknesses of each approach? (I've noticed that code collapsing and #regions don't seem to be supported. Also the pages get rather long and there is no longer the visual separation of client and server side code. Can you tell I have a preference?)

I realize that variations of this question have been asked before, but I haven't seen anyone actually spell out specifically the pros and cons of each method.

EDIT

Thank you everyone for your thought provoking answers. I'm still hoping for a list of strengths and weaknesses of each method. What are the actual features that each has (or doesn't have)?

Best Answer

There's no doubt in my mind that the code-behind or mvc models are superior for almost everything you want to do. However, even today I still find myself using inline code for most of my pages. Why? Because there's one big scenario where the inline scripts really shine.

If you have a large amount of legacy ASP Classic code that you don't want to just throw out, including a deep nesting structure, it all lives in one big application, and you want to share that application with your asp.net code, you can just drop inline asp.net pages right in your existing web file system and they'll just work.

This sounds like exactly where your other developer is coming from.

Related Topic