CodeFile 與 CodeBehind 的區別

 

CodeFile 與 CodeBehind 的區別

今天同事在把一個Web項目從 Web Site 升級成 Web Application 應用後部署的時候,報錯:

Server Error in '/' Application.
--------------------------------------------------------------------------------
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: The file '/index.aspx.cs' does not exist.
Source Error:
Line 1:  <%@ Page Language="C#" AutoEventWireup="true" CodeFile ="index.aspx.cs" Inherits="Community.IndexHomePage.index" %>
Line 2: 

原因就是不知道爲啥,採用系統升級工具,沒有把這個Web Page 的 CodeFile 變成 CodeBehind 。
而且僅僅只是這一個頁面沒升級,其他頁面都升級了。
再加上這兩個屬性都是 Code 開頭的,一不留神,就會覺得一個屬性。結果忙活了一回。

CodeFile 與 CodeBehind 在使用上是有很大區別的

先看 MSDN 上的說明:

CodeBehind
指定包含與頁關聯的類的已編譯文件的名稱。該屬性不能在運行時使用。
Specifies the name of the compiled file that contains the class associated with the control. This attribute is not used at run time.

CodeFile
指定指向頁引用的代碼隱藏文件的路徑。此屬性與 Inherits 屬性一起使用可以將代碼隱藏源文件與網頁相關聯。此屬性僅對編譯的頁有效。
Specifies a path to the referenced code-behind file for the control. This attribute is used together with the Inherits attribute to associate a code-behind source file with a user control. The attribute is valid only for compiled controls.

我這裏碰到的情況就是,整個Web 項目被編譯成了組件,但是 ASPX 頁面有如下的定義:
<%@ Page CodeFile="***"  Inherits="***" %>
這時候,ASP.net 就需要找 CodeFile 中指定的文件,以便動態編譯,但是找不到,所以就報上述錯誤了。

對於開發時,即 頁面的邏輯代碼 cs 文件存在的時候,下屬兩種寫法都沒有問題。
<%@ Page Language="C#" AutoEventWireup="true" Codebehind="index.aspx.cs" Inherits="Community.IndexHomePage.index" %>

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

但是部署到站點後,我們不會部署 cs 文件,這時候,後一種寫法就會報找不到文件的錯誤了。除非你把 cs 也部署出去,否則就會報編譯時錯誤,找不到文件...

發佈了52 篇原創文章 · 獲贊 4 · 訪問量 24萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章