ASP.NET CodeFileBaseClass属性与从System.Web.UI.Page继承

我刚从System.Web.UI.Page继承为我的页面创建了一个基类:

public abstract class PageBase : System.Web.UI.Page
{
    ...
}

当我注意到你也可以在ASP.NET视图中声明一个基页:

<%@ Page Language="C#" CodeFileBaseClass="PageBase.cs" CodeFile="page.aspx.cs"
    Inherits="page" %>

有人可以解释一种方法的优点和缺点是什么吗?你什么时候使用一个,或者它们都是一样的?如果你同时使用两者会怎么样?

解决方法

CodeFileBlassClass,CodeFile,继承与继承一起工作,而不是继承.

例如,指定CodeFile =“page.aspx.cs”而不存在page.aspx.cs将导致:

Parser Error Message: The file '/page.aspx.cs' does not exist.

假设存在page.aspx.cs,则指定CodeFileBaseClass =“PageBase.cs”而不存在PageBase.cs将导致:

Parser Error Message: Could not load type 'PageBase.cs'.

另一方面,您可以从PageBase继承而不指定CodeFileBaseClass属性.但是,当从基类引用页面上的控件时,这可能会导致意外行为.

引用Microsoft’s @Page MSDN Documentation

CodeFileBaseClass
Specifies the type name of a base class for a page and its associated code-behind class. This attribute is optional,but when it is used the
CodeFile attribute must also be present. Use this attribute when you want to implement a shared scenario,where you define common
fields (and optionally,associated events) in a base class to
reference the controls declared in a Web page. Because of the ASP.NET
code generation model,if you defined the fields in a base class
without using this attribute,at compile time new member deFinitions
would be generated for the controls declared in the Web page (within a
separate partial class stub),and your desired scenario would not
work. But if you use the CodeFileBaseClass attribute to associate
the base class with the page,and you make your partial class (its
name is assigned to the Inherits attribute and its source file is
referenced by the CodeFile attribute) inherit from the base class,then the fields in the base class will be able to reference the controls on the page after code generation.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....