This topic has not yet been rated - Rate this topic

CompilationMode Enumeration

Defines constants that specify how ASP.NET should compile .aspx pages and .ascx controls.

Namespace:  System.Web.UI
Assembly:  System.Web (in System.Web.dll)
public enum CompilationMode
Member name Description
Auto ASP.NET will not compile the page, if possible.
Never The page or control should never be dynamically compiled.
Always The page should always be compiled.

The CompilationMode enumeration values indicate whether a page or control should be compiled at run time. When no compilation mode is specified, the default value is Always. Setting a page to never compile using the Never attribute will increase performance by disqualifying the specified page in the compilation process. To define a page-specific CompilationMode attribute that specifies an .aspx, .ascx or .master file should not be compiled at the page level, you can use a directive like the following:

<%@ page compilationMode="never" %>

The compilation mode can also be specified in configuration files. The PagesSection configuration section object identifies page-specific configuration settings. The pages configuration section element can be declared at the machine, site, application, and subdirectory levels of the configuration files.

If the CompilationMode is set to Never and a page contains a script block or code construct that requires compilation, ASP.NET will return with an error and the page will not run.

The following code example demonstrates how to use the CompilationMode page directive to indicate that a page should never be compiled.

<%@ Page Language="C#" compilationMode="never" %>
<%@ Import namespace="System.IO" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>CompilationMode Example</title>
</head>
<body>
<form id="Form1" runat="server">

Script blocks are not allowed on this page. 
Any server side code that requires compilation 
contained in this page would cause the page to fail
</form>
</body>
</html>


Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Session problem solved
Here is my workaround. When you use CompilationMode="Never" and need to access Page.Session object, you must explicitely implement IRequiresSessionState interface in your System.Web.UI.Page derived class.

public partial class ProductDetailPage : System.Web.UI.Page, System.Web.SessionState.IRequiresSessionState
Master page affects.

If you set a pages value to "<%@ Page Language="VB" compilationMode="never" %>" and have code in a master page you will run into propblems with code executing in the master page. The solution will however compile without warnings in the Visual Studio 2005 and 2008 IDEs.

Here is an example error from Master page code where Content page has compilationMode is set to never.

Sub LoginStatus1_LoggingOut(ByVal sender AsObject, ByVal e As System.Web.UI.WebControls.LoginCancelEventArgs) Handles LoginStatus1.LoggingOut

' The following code corrects the situation where the web site retains the CslaPrinciple after the Logout button has been pressed.

Dim princ AsNew Iims.Security.IimsPrincipal

princ.LogOff()

' If you have a page directive of compilationMode="never" on a content page the following line of code will fail with a session error.

Session(

"CslaPrincipal") = Csla.ApplicationContext.User

System.Web.Security.FormsAuthentication.SignOut()

EndSub

Error Generated

Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule or a custom session state module is included in the <configuration>\<system.web>\<httpModules>section in the application configuration.