Background
 
ASP.Net Page directives provide the major role of processing web pages. This article explains what they are and when to use them. This article begins with the basics so beginners can also understand.
 
What directives are

Directives define the page configuration and properties that decide how the page will be processed. If the directives are not defined at the page level then the setting is inherited from web.config or machine.config file.

The following are the directive in ASP.Net.

Assembly 
 
The Assembly directive is used to register the assembly reference The following is the syntax of an assembly directive to be defined at the page level. Assembly directives have the following attributes: 
  • Name
  • Src
For example:
  1. <%@ Assembly Name="assemblyname" Src="SourceofAssembly.dll"%> 
Implements
 
The Implements directive is used to specify that the page implements interfaces, it has the following attribute:
  •  Interface
For example:
  1. <%@ Implements Interface="InterfaceName"%> 
Import
 
The Import directive imports a namespace into a page or user control explicitly, it has the following attribute:
  •  Namespace
For example:
  1. <%@ Import CodeFile="~/Default.aspx.cs" ClassName="" Inherits="" Language="C#"%> 
MasterType

The MasterType directive creates a strongly typed reference to the ASP.NET master page when the master page is accessed from the Master property. It has the following attributes:

  • TypeName
  • VirtualPath 
  1. <%@MasterType TypeName="Name"  VirtualPath="virtualPathofFile"%> 
OutPutCache
 
The OutPutCache directive configures the caching of the page that defines what type of caching is used. It has the following attributes: 
  • Duration
  • Location
  • ServerAndClient 
  • Shared
  • VaryByControl
  • VaryByCustom
  • VaryByHeader
  • VaryByParam 
  • VaryByContentEncoding
  • CacheProfile
  • NoStore
  • SqlDependency
  • ProviderName
For example:
  1. <%@OutPutCache CacheProfile="ProFileName" Duration="defineduration" SqlDependency="definedependency" %> 
Page
 
The Page directive is the most commonly used directives that define the entire configuration of the page.
  • Async
  • AsyncTimeOut
  • AspCompat
  • AutoEventWireup
  • Buffer
  • ClassName
  • ClientIDMode
  • ClientTarget
  • CodeBehind
  • CodeFile
  • CodeFileBaseClass
  • CodePage
  • CompilationMode
  • CompilerOptions
  • ContentType
  • Culture
  • Debug
  • Description
  • EnableSessionState
  • EnableTheming
  • EnableViewState
  • EnableViewStateMac
  • ErrorPage
  • Explicit
  • Inherits
  • Language
  • LCID
  • LinePragmas
  • MaintainScrollPositionOnPostback
  • MasterPageFile
  • MetaDescription
  • MetaKeywords
  • ResponseEncoding
  • SmartNavigation
  • Src
  • Strict
  • StyleSheetTheme
  • TargetSchema
  • Theme
  • Title
  • Trace
  • TraceMode
  • Transaction
  • UICulture
  • ValidateRequest
  • ViewStateEncryptionMode
  • ViewStateMode
  • WarningLevel
For example:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 
PreviousPageType

The PreviousPageType directive defines the Previous Page reference and access the properties of the child page. It has the following attributes:

  • TypeName
  • VirtualPath 
For example:
  1. <%@PreviousPageType TypeName="TyepOfFile" VirtualPath="PathOfTheFile"%> 
Reference
 
The Reference directive indicates that another user control, page source file or arbitrary file located at some virtual path should be dynamically compiled and linked against the current ASP.NET file such as Web page, user control, or master page in which this directive is declared. It has the following attributes:
  • Control
  • PageVirtual
  • Path
For example:
  1. <%@Reference Control="UrlOfControl" Page="PageUrl" VirtualPath="VirtualpathOfFile"%> 
Register
 
The Register directive registers the user and custom control on a page. It has the following attributes:
  • Assembly
  • Namespace
  • Src
  • TagName
  • TagPrefix
For example:
  1. <%@Register Assembly="assemblyName" Namespace="NameSpaceFile" Src="SourceFileUrl" TagName="TagName" TagPrefix="Prefix"%> 
Master
 
The Master directive is used for master page configuration. It has the following attributes:
  • AutoEventWireup
  • CodeFile 
  • CompilationMode
  • CompilerOptions
  • Debug
  • Description
  • EnableTheming
  • EnableViewState
  • Explicit
  • Inherits
  • Language
  • LinePragmas
  • MasterPageFile
  • Src
  • Strict
  • WarningLevel

For example:
  1. <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %> 
Control

The Control directive is used for user control configuration. It has the following attributes:

  • AutoEventWireup
  • ClassName
  • ClientIDMode
  • CodeBehind
  • CodeFile
  • CodeFileBaseClass
  • CompilationMode
  • CompilerOptions
  • Debug
  • Description
  • EnableTheming
  • EnableViewState
  • Explicit
  • Inherits
  • Language
  • LinePragmas
  • Src
  • Strict
  • TargetSchema
  • WarningLevel
For example:
  1. <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %> 
From all the preceding examples, we have learned about the page directives and their importance.
 
Note
  • For the detailed practical approach, create sample pages using the preceding directives.
Summary

For all the examples above, we have learned about the directives. I hope this article is useful for all readers. If you have a suggestion then please contact me.

Next Recommended Readings