ASP.NET指令详解

一、
ASP.NET指令是用于配置和控制ASP.NET页面行为的关键工具,它们在ASP.NET页面或用户控件中以特殊语法形式存在,通常位于文件顶部,用于设置页面的特定属性和配置信息,本文将详细介绍ASP.NET中的各种指令及其用法。
二、常用指令及说明
@ Page 指令
定义**:
@ Page 指令用于定义ASP.NET页面分析器和编译器使用的页特定属性,它只能包含在 .aspx 文件中。
主要属性**:
Language:指定页面使用的编程语言(如C#, VB等)。
AutoEventWireup:指示是否自动绑定页面事件,默认为true。
CodeFile:指定后台代码文件路径。
Debug:指示是否使用调试符号编译页面,默认为false。
Inherits:指定页面继承的基类。
Buffer:是否启用HTTP响应缓冲,默认为true。
示例**:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
@ Control 指令
定义**:
@ Control 指令用于定义用户控件(.ascx文件)特定的属性,它只能在用户控件中使用。
主要属性**:
ClassName:指定控件的类名。
CodeFile:指定后台代码文件路径。
Runat:指定控件在服务器端运行,固定值为server。
示例**:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyControl.ascx.cs" ClassName="MyControl" %>
@ Import 指令
定义**:
@ Import 指令用于将命名空间显式导入到页面或用户控件中,这有助于简化代码中的类型引用。
主要属性**:
Namespace:要导入的命名空间。
示例**:
<%@ Import Namespace="System.Data" %>
@ Implements 指令
定义**:
@ Implements 指令用于指示当前页面或用户控件实现指定的 .NET Framework 接口。
主要属性**:
Interface:要实现的接口名称。
示例**:
<%@ Implements Interface="IMyInterface" %>
@ Register 指令
定义**:
@ Register 指令用于在页面或控件中创建自定义标签前缀,以便在页面中使用自定义控件或服务器控件。
主要属性**:
TagPrefix:与命名空间关联的前缀。
Namespace:与前缀关联的命名空间。
Assembly:程序集的名称。
Src:用户控件的位置。
TagName:与类名关联的别名。
示例**:
<%@ Register TagPrefix="uc" Namespace="MyControls" Assembly="MyControls" %>
@ Assembly 指令
定义**:

@ Assembly 指令用于将程序集链接到当前页面或用户控件上。
主要属性**:
AssemblyName:要链接的程序集名称。
示例**:
<%@ Assembly Name="MyAssembly" %>
@ MasterType 指令
定义**:
@ MasterType 指令用于将类名与页面关联起来,从而获得包含在特定母版页中的强类型化的引用或成员。
主要属性**:
VirtualPath:母版页的虚拟路径。
TypeName:母版页的类型名称。
示例**:
<%@ MasterType VirtualPath="~/MasterPage.master" TypeName="MasterPage" %>
@ Reference 指令
定义**:
@ Reference 指令用于将页面或用户控件链接到当前页面或用户控件上的其他程序集。
主要属性**:
Assembly:要链接的程序集名称。
Src:源代码文件的位置。
示例**:
<%@ Reference Assembly="MyOtherAssembly" %>
9. @ PreviousPageType 指令
定义**:
@ PreviousPageType 指令允许ASP.NET页面处理应用程序中另一个页面的回送信息。
主要属性**:
VirtualPath:先前页面的虚拟路径。
TypeName:先前页面的类型名称。
示例**:
<%@ PreviousPageType VirtualPath="~/PreviousPage.aspx" TypeName="PreviousPage" %>
@ Master 指令
定义**:
@ Master 指令用于指定母版页,这个指令只能与母版页 (.master) 一起使用。
主要属性**:
Page:母版页的文件路径。
Inherits:母版页的基类名称。
MasterPageFile:母版页的文件路径。
AutoEventWireup:是否自动绑定母版页事件。
Debug:是否使用调试符号编译母版页。
Language:指定编程语言。
示例**:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>
@ OutputCache 指令
定义**:
@ OutputCache 指令用于控制页面或用户控件的输出缓存策略,它帮助提升页面性能,通过缓存动态生成的内容来减少服务器负载。
主要属性**:
Duration:缓存持续时间(秒)。
VaryByParam:根据查询字符串参数变化缓存内容。
VaryByControl:根据用户控件的变化缓存内容。
VaryByHeader:根据HTTP头信息变化缓存内容。

VaryByCustom:根据自定义逻辑变化缓存内容。
Enabled:是否启用缓存,默认为true。
Location:缓存存储位置(客户端、服务器、任意)。
NoStore:禁止代理服务器缓存页面。
Shared:指示是否可以共享缓存项。
SqlDependency:指定SQL数据库依赖项,用于刷新缓存。
CacheProfile:指定缓存配置文件名。
UseExpires:指定过期时间。
UseKernelCacheProfile:使用内核缓存配置文件。
UseResponseCacheControl:使用响应缓存控制标头。
UseValidation:使用验证模块确定缓存项是否有效。
UseClientCaching:指示是否使用客户端缓存。
UseProxyCachingOnLocal:指示是否在本地使用代理缓存。
VaryByContentEncoding编码变化缓存内容。
VaryByHttps:根据HTTPS协议变化缓存内容。
VaryByQueryParams:根据查询参数变化缓存内容。
VaryByUserCustom:根据用户自定义条件变化缓存内容。
VaryBySessionVariables:根据会话变量变化缓存内容。
VaryByCookie:根据cookie变化缓存内容。
VaryByAcceptEncoding:根据接受编码变化缓存内容。
VaryByHeaders:根据HTTP头变化缓存内容。
VaryByMetadataToken:根据元数据令牌变化缓存内容。
VaryByUser:根据用户变化缓存内容。
VaryByProtocol:根据协议变化缓存内容。
VaryByScheme:根据模式变化缓存内容。
VaryByFragment:根据URL片段变化缓存内容。
VaryByHost:根据主机名变化缓存内容。
VaryByAuthorizationCookie:根据授权cookie变化缓存内容。
VaryByRouteInfoCollection:根据路由信息***变化缓存内容。
VaryByCustomHostHeader:根据自定义主机头变化缓存内容。
VaryByCustomHostValue:根据自定义主机值变化缓存内容。
VaryByCustomHostParameter:根据自定义主机参数变化缓存内容。
VaryByCustomHostTokens:根据自定义主机标记变化缓存内容。
VaryByCustomHostVariables:根据自定义主机变量变化缓存内容。
VaryByCustomHostAttributes:根据自定义主机属性变化缓存内容。
VaryByCustomHostProperties:根据自定义主机属性变化缓存内容。
VaryByCustomHostValues:根据自定义主机值变化缓存内容。
**VaryByCustomHostTokensAndValuesAndAttributesAndPropertiesAndVariablesAndParametersAndHeadersAndMetadataTokensAndFragmentsAndProtocolsAndSchemesAndAuthorizationCookiesAndRouteInfoCollectionsAndCustomHostHeadersAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomHostTokensAndCustomHostVariablesAndCustomHostAttributesAndCustomHostPropertiesAndCustomHostValuesAndCustomhostVariablesAndCustomhostAttributesAndcustomhostProperties
到此,以上就是小编对于“asp.net 指令”的问题就介绍到这了,希望介绍的几点解答对大家有用,有任何问题和不懂的,欢迎各位朋友在评论区讨论,给我留言。