The application of the ASP.net System.Web.HttpContext class current attribute __.net

Source: Internet
Author: User
Tags httpcontext

1,. NET HTTP request

. NET and ASP's operating mechanism is very different.

The ASP's request and response is that the ASP page is requested and then interpreted line by row. is based on the process. Most of the files are included, you need to introduce the file first.

and. Net, after the page is requested, the first is to 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. The two items loaded, All need to be configured in the Webconfig file. This is what you know about URL transfers. After completing these, the page that gives the response starts.

In the job of giving a response page,. NET shows the biggest difference from ASP. NET is an object-oriented language, and is a class based language. No class is difficult to write a meaningful. NET program. If it is written, it is an ASP version.

2,. Basic file structure in net (inheritance relationship)
First of all, you see whether it's 1.1 or 2.0, sites created with vs each aspx file corresponds to a CS file (or VB and other languages). And the CS file in the class and file name, if you meet the keyword will automatically make the class name before adding a ' _ ', for example: _ The CS file created by Default inherits from the System.Web.UI.Page class and can/must inherit the class. The reason is that the ASPX file inherits the CS file.

Just take the Default.aspx,default.aspx.cs file.
Default.ASPx.cs contains the class _default, inherited characters System.Web.UI.Page
_default:system.web.ui.page through public class
{
private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
}
}
can be seen.
The default.aspx file, while not clarifying which class to use, has actually inherited from the _default class.
<% @ Page Language = "C #" Codebehind = "Default.ASPx.cs" AutoEventWireup = "false" Inherits = "Mytest._defaul T "%>
<! 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" >
< body ms_positioning = "GridLayout" >
< form ID = "Form1" method = "post" runat = "server" >
< FONT face = "XXFarEastFont-></" FONT >
</form >
</Body >
</HTML > above code the first sentence finally inherits= "Mytest._default" indicates this. This file was built by VS2003. Net1.1,mytest is the namespace I use.

3. Operation mechanism of class
Having said so much, in fact we still did not talk about the System.Web.HttpContext class. But what we are saying now is very helpful to the use of the System.Web.HttpContext class.

We visited the Default.aspx, so we just access the Default.aspx file, or the class. The answer, of course, is not. NET instantiates the parent class when the subclass is requested. First, the fields of the parent class are established, and then the constructors.

public class _default:system.web.ui.page
{
private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
}

Web Forms Designer generated Code #region Web Forms Designer generated code
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

/**////<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion
As you can see from the class above, the class has no fields or constructors. Instantiating it has to instantiate the System.Web.UI.Page class first. The System.Web.UI.Page class is not discussed in this article. The OnInit method is first executed when instantiating the _default class. It rewrites the System.Web.UI.Page.OnInit. The InitializeComponent method is then invoked, and the InitializeComponent method invokes the Load event and executes the Page_Load method.

Before the Page_Load method is executed, many objects of the System.Web.UI.Page class cannot be used, such as: Application,session.

4, the application of System.Web.HttpContext class current attribute
Finally speaking of the focus of this article, perhaps this part will not say a lot, but the above is really for the service here.

Also not much said, too long articles generally no one to see, not to mention I write more superficial, and not wonderful. Here are two examples. One is the authentication issue (session), and the other is global (application).

1), session
After the user log in generally through Cookice or session to control the rights. But it doesn't feel good enough to use a single control, such as Ascx. So, this time the System.Web.HttpContext.Current will be able to play a role.

For example, you can allow all pages that require authentication to inherit from the MPage class. As follows:
Using System;
Using System.Web;

Namespace MyTest
{
/**////<summary>
Summary description of the MyTest.
</summary>
public class MPage:System.Web.UI.Page
{
Public MPage ()
{
//
TODO: Add constructor logic here
//
if (httpcontext.current.session["mysession"]==null | | httpcontext.current.session["MySession"]==string.empty)

Related Article

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.