LoginSignup
8
6

More than 5 years have passed since last update.

CodeBehind vs CodeFile (1)

Last updated at Posted at 2015-06-14

背景

いままでのASP.NET開発では、特に意識せずにVisual Studio既定のCodeBehind属性を使っていました。
ただ、Sitecoreを調べていくうちに、あえてCodeBehindからCodeFileに変更している事例が散見されました。
このため、事例の背景を正しく理解するためにCodeBehindとCodeFileの違いについて調べてみました。

準備

  1. ASP.NET 空のWeb アプリケーションを作成します。
  2. Webフォーム「CodeBehind.aspx」と「CodeFile.aspx」を追加します。
    キャプチャ.PNG

  3. ローカルIISを使うように設定します。

  4. 以下のようなコードを入力します。

●CodeBehind.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CodeBehind.aspx.cs" Inherits="WebApplication.WebForm" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="lbl" runat="server" Text="default" />
        </div>
    </form>
</body>
</html>

●CodeBehind.aspx.cs

using System;

namespace WebApplication {
    public partial class WebForm : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            lbl.Text = "code bihind!!(●ω●;)";
        }
    }
}

●CodeFile.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="CodeFile.aspx.cs" Inherits="WebApplication.CodeFile" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:Label ID="lbl" runat="server" Text="default" />
        </div>
    </form>
</body>
</html>

●CodeFile.aspx.cs

using System;

namespace WebApplication {
    public partial class CodeFile : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            lbl.Text = "code file...(´・ω・`)";
        }
    }
}

実験と結果

ビルドしないで、各aspxにリクエストしてみる。

1) CodeBehind.aspx
 → エラーとなる。
キャプチャ2.PNG

2) CodeFile.aspx
 → エラーとならない。キャプチャ3.PNG
→CodeFileの動的コンパイルのため

サーバーコントロールを各aspxに貼り付けてみる。

Labelコントロール(ID:Label1)をもう一つ、ツールボックスからドラッグ&ドロップで貼り付けてみる。
1) CodeBehind.aspx
 → CodeBehind.aspx.designer.csに自動的にLabel1の定義が記述される。
2) CodeFile.aspx
 → CodeFile.aspx.designer.csにLabel1の定義が記述されない。

 //CodeBehindだと書かれます
 protected global::System.Web.UI.WebControls.Label Label1;

ビルドしてできたDLLをILSPYで逆コンパイルしてみる。

CodeBehindクラスもCodeFileクラスも両方存在する。
4.PNG
(これはcsファイルのビルドアクションが既定で"コンパイル"であり、そのままにしていたため。CodeFile.csとCodeFile.designer.csのビルドアクションを"コンテンツ"に変えれば、DLLには含まれなかった)

ビルド後にCodeFile.csのロジックを変更してリクエストしてみる。

 ビルド後にCodeFile.csを以下のように書き換える。
 (※CodeFile.csのビルドアクションはコンパイルにしておく)

using System;
namespace WebApplication {
    public partial class CodeFile : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {
            lbl.Text = "code file!!!(`・ω・´)"; //←ここを変更
        }
    }
}

ビルドしないでリクエストしてみると、変更後のコードが実行された。

5.PNG
→ dll内の処理よりcsファイルの処理が優先されることがわかる

ビルド後にCodeFile.csをリネームしてリクエストしてみる。

Codefile.cs → -Codefile.cs
Codefile.designer.cs → -Codefile.designer.cs

エラーとなる。
6.PNG
→ そもそもdllを見に行ってないことがわかる

まとめ

  1. CodeBehind属性は予めビルド済みのDLLが必須。csファイルは不要(あっても使わない)
  2. CodeFile属性は予めビルド済みのDLLは不要(あっても使わない)。csファイルは必須。
  3. CodeBehind属性は貼り付けたサーバコントロールの参照がdesigner.csに自動生成される。CodeFile属性は自動生成されない。

関連

CodeBehind vs CodeFile (2)

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6