ASP. NET system. Web. httpcontext class current attribute

Source: Internet
Author: User

1.. Net HTTP Request

The operating mechanism of. NET is very different from that of ASP.

ASP requests and responses are: ASP pages are requested and explained line by line. They are process-based. They contain files at most, and files must be introduced first.

And. net, after the page is requested, first load the HTTP module, which is not the focus of this Article. Then load the HTTP
Handler. This is not the focus of this Article. To load these two items, You need to configure them in the webconfig file. You know that URL redirection is implemented in this way.
The response page.

In the work on the response page ,. NET shows the biggest difference with ASP .. net is an object-oriented language and a class-based language. it is difficult to write meaningful without classes. netProgram. If it is written, it is an ASP version.

2. Basic file structure (inheritance relationship) in. net)
First, we can see that in both 1.1 and 2.0, each aspx file of the website created with vs corresponds to a CS file (or other language such as VB ). the classes in CS files are generally the same as those in files,
If you encounter a keyword, the class name will be automatically prefixed with '_', for example, _ default. the created CS file inherits from system. web. UI. page class, and only
Yes or no. The reason is that the aspx file inherits the CS file.

Take the default. aspx, default. aspx. CS file as an example.
Default. aspx. CS contains the class _ default, which inherits the word system. Web. UI. Page.
Pass

Public class _ default: system. Web. UI. Page
{
Private void page_load (Object sender, system. eventargs E)
{
// Place the user here Code To initialize the page
}
}
.
While the default. aspx file does not clarify which class to use, it actually inherits from the _ default class.
<% @ Page Language = "C #" codebehind = "default. aspx. cs" autoeventwireup = "false" inherits = "mytest. _ default" %>
<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> default </title>
<Meta name = "generator" content = "Microsoft Visual Studio. NET 7.1">
<Meta name = "code_language" content = "C #">
<Meta name = "vs_defaultclientscript" content = "JavaScript">
<Meta name = "vs_targetschema" content = "http://schemas.microsoft.com/intellisense/ie5
">
</Head>
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Font face = ""> </font>
</Form>
</Body>
</Html> In the first sentence of the above Code, inherits = "mytest. _ default" indicates this.

3. Class Running Mechanism

If we access default. aspx, do we only access the default. aspx file or the class? Of course not .. When a subclass is requested in. net, the parent class is instantiated first. First, create the fields of the parent class, and then the constructor.

Public class _ default: system. Web. UI. Page
{
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
}


Code generated by web form designer # code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/** // <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()

{
This. Load + = new system. eventhandler (this. page_load );

}
# Endregion

} As shown in the preceding class, this class has no fields or constructor. To instantiate it, you must first instantiate the system. Web. UI. Page class.
The system. Web. UI. Page class is not discussed in this article. The oninit method is first executed when the _ default class is instantiated. It is rewritten
System. Web. UI. Page. oninit. Then call the initializecomponent method, while initializecomponent
The method calls the load event and executes the page_load method.

Before the page_load method is executed, many objects in the system. Web. UI. Page class cannot be used, such as application and session.

4. Use of the current attribute of the system. Web. httpcontext class

Here are two examples. One is the authentication question (Session), and the other is the global session (application ).

1) session
After logging on to the system, you must use cookice or session to control permissions. However, it is not good to use a control (such as ascx. In this case, system. Web. httpcontext. current can be used.

For example, you can make all pages that require authentication inherit from the mpage class. As follows:
Using system;
Using system. Web;

namespace mytest
{< br>/** ///


/// Summary of mytest.
//
public class mpage: system. web. UI. page
{< br> Public mpage ()
{< br> //
// todo: add the constructor logic here
//
If (httpcontext. current. session ["mysession"] = NULL | httpcontext. current. session ["mysession"] = string. empty)
httpcontext. current. response. redirect ("login. aspx ", true); // if the session verification fails, the logon page is displayed.

}< BR >}< br> and then in default. aspx. the _ default class in the CS file inherits the class
using system;
using system. collections;
using system. componentmodel;
using system. data;
using system. drawing;
using system. web;
using system. web. sessionstate;
using system. web. ui;
using system. web. UI. webcontrols;
using system. web. UI. htmlcontrols;

Namespace mytest
{
/** // <Summary>
/// _ Default abstract description.
/// </Summary>
Public class _ default: mpage
{
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
}

Code generated by web form designer # code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/** // <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );


}
# Endregion
}
}
In this way, authentication is required when accessing the default. aspx file. If the Web. config or DLL file has been modified after authentication, the session will be canceled (pay attention to this when debugging ).

2. Application

Application can be used as follows:
Using system;
Using system. Web;

Namespace mytest
{
/** // <Summary>
/// Summary of mytest.
/// </Summary>
Public class mpage: system. Web. UI. Page
{
Public mpage ()
{
//
// Todo: add the constructor logic here
//
Try
{
If (httpcontext. Current. application. Count <1)
Httpcontext. Current. application. Add ("title", "my website"); // run the following command when the application is not created.
}
Catch
{
Httpcontext. Current. response. Redirect ("Err. htm", true); // The error page is displayed.
}
}

Public String my_title
{< br> Get
{< br> return application [" title "]. tostring ();
}< BR >}< br> default. aspx. CS inheritance
using system;
using system. collections;
using system. componentmodel;
using system. data;
using system. drawing;
using system. web;
using system. web. sessionstate;
using system. web. ui;
using system. web. UI. webcontrols;
using system. web. UI. htmlcontrols;

Namespace mytest
{
/** // <Summary>
/// _ Default abstract description.
/// </Summary>
Public class _ default: mpage
{
Private void page_load (Object sender, system. eventargs E)
{
// Place user code here to initialize the page
}

Code generated by web form designer # code generated by region web Form Designer
Override protected void oninit (eventargs E)
{
//
// Codegen: This call is required by the ASP. NET web form designer.
//
Initializecomponent ();
Base. oninit (E );
}

/** // <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void initializecomponent ()
{
This. Load + = new system. eventhandler (this. page_load );


}
# Endregion
}
}
Use the default. aspx File
<% @ Page Language = "C #" codebehind = "default. aspx. cs" autoeventwireup = "false" inherits = "mytest. _ default" %>

<! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en">
<HTML>
<Head>
<Title> <% = my_title %> </title>
<Meta name = "generator" content = "Microsoft Visual Studio. NET 7.1">
<Meta name = "code_language" content = "C #">
<Meta name = "vs_defaultclientscript" content = "JavaScript">
<Meta name = "vs_targetschema" content = "http://schemas.microsoft.com/intellisense/ie5
">
</Head>
<Body ms_positioning = "gridlayout">
<Form ID = "form1" method = "Post" runat = "server">
<Font face = ""> </font>
</Form>
</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.