Compartilhar via


@ Page

Defines page-specific (.aspx file) attributes used by the ASP.NET page parser and compiler.

<%@ Page attribute="value" [attribute="value"...] %>

Attributes

  • AspCompat
    When set to true, this allows the page to be executed on a single-threaded apartment (STA) thread. This allows the page to call STA components, such as a component developed with Microsoft Visual Basic 6.0. Setting this attribute to true also allows the page to call COM+ 1.0 components that require access to unmanaged Active Server Pages (ASP) built-in objects. These are accessible through the ObjectContext object or the OnStartPage method. The default is false.

    Note   Setting this attribute to true can cause your page's performance to degrade. For more information, see the Remarks section.

  • AutoEventWireup
    Indicates whether the page's events are autowired. true if event autowiring is enabled; otherwise, false. The default is true. For more information, see Web Server Control Event Model.

  • Buffer
    Determines whether HTTP response buffering is enabled. true if page buffering is enabled; otherwise, false. The default is true.

  • ClassName
    Specifies the class name for the page that will be dynamically compiled automatically when the page is requested. This value can be any valid class name but should not include a namespace.

  • ClientTarget
    Indicates the target user agent for which ASP.NET server controls should render content. This value can be any valid user agent or alias.

  • CodeBehind
    Specifies the name of the compiled file that contains the class associated with the page. This attribute is used by the Visual Studio .NET Web Forms designer. It tells the designer where to find the page class so that the designer can create an instance of it for you to work with at design time. For example, if you create a Web Forms page in Visual Studio called WebForm1, the designer will assign the Codebehind attribute the value of WebForm1.aspx.vb, for Visual Basic, or WebForm1.aspx.cs, for C#. This attribute is not used at run time.

  • CodePage
    Indicates the code page value for the response.

    Note   If you created your Web Forms page using a code page other than the default code page of the Web server on which the page will run, you must set this attribute. The value you enter should be the code page for the computer on which you created the page. For more information about code pages, search the Microsoft Developers Network (MSDN) at https://msdn.microsoft.com.

  • CompilerOptions
    A string containing compiler options used to compile the page. In C# and Visual Basic .NET, this is a sequence of compiler command-line switches.

  • ContentType
    Defines the HTTP content type of the response as a standard MIME type. Supports any valid HTTP content-type string. For a list of possible values, search for MIME in MSDN at https://www.microsoft.com/technet.

  • Culture
    Indicates the culture setting for the page. For information about culture settings and possible culture values, see the CultureInfo class.

  • Debug
    Indicates whether the page should be compiled with debug symbols. true if the page should be compiled with debug symbols; otherwise, false.

  • Description
    Provides a text description of the page. This value is ignored by the ASP.NET parser.

  • EnableSessionState
    Defines session-state requirements for the page. true if session state is enabled; ReadOnly if session state can be read but not changed; otherwise, false. The default is true. These values are case-insensitive. For more information, see Session State.

  • EnableViewState
    Indicates whether view state is maintained across page requests. true if view state is maintained; otherwise, false. The default is true.

  • EnableViewStateMac
    Indicates that ASP.NET should run a machine authentication check (MAC) on the page's view state when the page is posted back from the client. true if view state should be MAC checked; otherwise, false. The default is false.

    Note   A view-state MAC is an encrypted version of the hidden variable that a page's view state is persisted to when sent to the browser. When you set this attribute to true, the encrypted view state is checked to verify that it has not been tampered with on the client.

  • ErrorPage
    Defines a target URL for redirection if an unhandled page exception occurs.

  • Explicit
    Determines whether the page is compiled using the Visual Basic Option Explicit mode. true indicates that the Visual Basic explicit compile option is enabled and that all variables must be declared using a Dim, Private, Public, or ReDim statement; otherwise, false. The default is false.

    Note   This attribute is ignored by languages other than Visual Basic .NET. Also, this option is set to true in the Machine.config configuration file. For more information, see Machine Configuration Files.

  • Inherits
    Defines a code-behind class for the page to inherit. This can be any class derived from the Page class. For information about code-behind classes, see Web Forms Code Model.

  • Language
    Specifies the language used when compiling all inline rendering (<% %> and <%= %>) and code declaration blocks within the page. Values can represent any .NET-supported language, including Visual Basic, C#, or JScript .NET.

  • LCID
    Defines the locale identifier for the Web Forms page.

    Note   The locale identifier is a 32-bit value that uniquely defines a locale. ASP.NET uses the default locale of the Web server unless you specify a different locale for a Web Forms page by using this attribute. For more information about locales, search MSDN at https://msdn.microsoft.com.

  • ResponseEncoding
    Indicates the response encoding of page content. Supports values from the Encoding.GetEncoding method.

  • Src
    Specifies the source file name of the code-behind class to dynamically compile when the page is requested. You can choose to include programming logic for your page either in a code-behind class or in a code declaration block in the .aspx file.

    Note   RAD designers, such as Visual Studio .NET, do not use this attribute. Instead, they precompile code-behind classes and then use the Inherits attribute.

  • SmartNavigation
    Indicates whether the page supports the smart navigation feature of Internet Explorer 5.5 or later..

    Note   For more information about smart navigation, see the Remarks section.

  • Strict
    Indicates that the page should be compiled using the Visual Basic Option Strict mode. true if Option Strict is enabled; otherwise, false. The default is false.

    Note   This attribute is ignored by languages other than Visual Basic .NET.

  • Trace
    Indicates whether tracing is enabled. true if tracing is enabled; otherwise, false. The default is false. For more information, see ASP.NET Trace.

  • TraceMode
    Indicates how traces messages are to be displayed for the page when tracing is enabled. Possible values are SortByTime and SortByCategory. The default, when tracing is enabled, is SortByTime. For more information about tracing, see ASP.NET Trace.

  • Transaction
    Indicates whether transactions are supported on the page. Possible values are Disabled, NotSupported, Supported, Required, and RequiresNew. The default is Disabled.

  • UICulture
    Specifies the UI culture setting to use for the page. Supports any valid UI culture value.

  • ValidateRequest
    Indicates whether request validation should occur. If true, request validation checks all input data against a hard-coded list of potentially dangerous values. If a match occurs, an HttpRequestValidationException Class is thrown. The default is true.

    This feature is enabled in the machine configuration file (Machine.config). You can disable it in your application configuration file (Web.config) or on the page by setting this attribute to false.

    Note   This functionality helps reduce the risk of cross-site scripting attacks for straightforward pages and ASP.NET applications. An application that does not properly validate user input can suffer from many types of malformed input attacks, including cross-site scripting and SQL Server injection attacks. There is no substitute for carefully evaluating all forms of input in an application and making sure that they are either properly validated or encoded, or that the application is escaped prior to manipulating data or sending information back to the client. For more information about cross-site scripting, see http://www.cert.org/advisories/CA-2000-02.html.

  • WarningLevel
    Indicates the compiler warning level at which you want the compiler to abort compilation for the page. Possible values are 0 through 4. For more information, see the CompilerParameters.WarningLevel Property property.

Remarks

This directive can be used only in Web Forms pages. You can include only one @ Page directive per .aspx file. To define multiple attributes for the directive, use a space-separated list (do not include a space on either side of the equal sign of a specific attribute, as in trace="true").

Smart navigation is an ASP.NET feature that is supported in Internet Explorer 5.5 and later browsers. It allows a page to be refreshed while maintaining scroll position and element focus between navigations, causing only a single page to be stored in the browser's history, and without the common flicker associated with refreshing a Web page. Smart navigation is best used with ASP.NET pages that require frequent postbacks but with visual content that does not change dramatically on return. Consider this carefully when deciding whether to set this attribute to true.

When the AspCompat attribute is set to true for a page, if you use a constructor to create a COM component before the request is scheduled, it will run on a multithreaded apartment (MTA) thread. Doing this causes significant Web server performance degradation. To avoid this problem, create COM components only from within one of the Page events (such as Page_Load, Page_Init, and so on) or one of the Page methods. Be sure as well that the objects are not created at page construction time. The following examples demonstrate the incorrect and correct way to instantiate a COM object in an AspCompat page. MyComObject is the component, and comObj is the instance of the component.

<%@ Page AspCompat="TRUE" language="C#" %>
<script runat="server" >
    // Avoid this when using AspCompat.
    MyComObject comObj = new MyComObject();

    public void Page_Load()
    {
    //   comObj.DoSomething()
    } 
</script>
 
[Visual Basic]
<%@ Page ASPCOMPAT="TRUE" language="VB" %>
<script runat="server" >
    ' Avoid this when using AspCompat.
    Dim comObj As MyComObject = New MyComObject()

    Public Sub Page_Load()
       ' comObj.DoSomething()
    End Sub
</script>

The recommended way to instantiate a COM object in a page with AspCompat enabled.

<%@ Page AspCompat="true" language="C#" %>
<script runat="server" >

MyComObject comObj;

public void Page_Load(){
   // Use comObj here when the code is running on the STA thread pool.
   comObj = New MyComObject();
   // comObj.DoSomething();
}

[Visual Basic]
<%@ Page AspCompat ="true" language="VB" %>
<script runat="server" >

    Dim comObj As MyComObject 

    Public Sub Page_Load()
       comObj = New MyComObject()
       ' comObj.DoSomething()
    End Sub
</script>

Example

The following code instructs the ASP.NET page compiler to use Visual Basic as the inline code language and sets the default HTTP MIME ContentType transmitted to the client to "text/xml".

<%@ Page Language="VB" ContentType="text/xml" %>

See Also

ASP.NET Web Forms Syntax | Introduction to Web Forms Pages | Directive Syntax