Use response and request objects in non-xxx. aspx. CS files

Source: Internet
Author: User
ArticleDirectory
    • 1. app_code/accounts. CS
    • 2. default. aspx. CS
    • 3. login. aspx. CS

Use the response and request objects in non-xxx. aspx. CS files, that is, in the codefile file of the aspx file (code behind separatedCodeFile.

I believe that users with ASP. NET development experience are absolutely familiar with response and request objects. Their functions and benefits are not covered in this article. Normally we are in XXX. aspx. the CS file directly uses the response and request objects. For example, if we have a page (usually called a background management page) that requires logon, we can directly access this page. IfProgramThe logon page is displayed when the user is not logged on. The Code is as follows:


Protected   Void Page_load ( Object Sender, eventargs E)
{
If (Session [ " Username " ] ! =   Null )
{
//
// Todo: add the login verification logic here
//
}
Else
{
Response. Redirect ( " Login. aspx " );
}
}

We can see that we have used response and session objects above. Now we assume that we have many pages that use this code to determine the user's logon status, do we need to repeatedly write the same code on different pages? Obviously, this is not scientific. We need to refactor and reuse the code.

We usually put some practical methods in the app_code folder for reuse. Can the session verification and page conversion methods be encapsulated in a CS file in the app_code folder? In the xxx. aspx. the CS file is called directly as follows. The address of the login page to be switched on that day also needs to change the CS file in the app_code folder instead of every XXX. aspx. modify all Cs files:


Protected   Void Page_load ( Object Sender, eventargs E)
{
Accounts. checklogin ();
}

The above accounts. checklogin (); method is used to detect user logon, which contains the jump page processing logic.

The following code is displayed in the automatically generated XXX. aspx. CS file.


Public   Partial   Class _ Default: system. Web. UI. Page
{
Protected   Void Page_load ( Object Sender, eventargs E)
{
Response. Write ( " Some text! " );
}
}

Move the mouse over "response" in Visual Studio, And the IDE prompts that the "response" object here is system. web. UI. page member. Does this mean that we create a class in the CS file in the app_code folder and make the class inherit from system. web. UI. page class, then it can be the same as in XXX. aspx. in the CS file, how does one directly use objects such as response and session? For example, as shown below (app_code/accounts. CS ):


Public   Class Accounts: system. Web. UI. Page
{
Public Accounts ()
{
Response. Write ( " Some text! " );
}
}

The above code is only compiled and can pass, but after the program runs, you will find an error and report "The response is unavailable in this context ". There is a concept of "context". The following "httpcontext" refers to "HTTP context. The error is reported here. According to msdn, due to the stateless nature of HTTP, web applications need to track context fragments. For example, in XXX. aspx. you can use "request. URL "to get the URL Information of the current request, and if you put the request. the URL method is directly defined in a CS file in the app_code folder, and then called directly outside. The program does not know what you want is the response of the page, that is, there is no reference environment, because the CS file in the app_code folder is not directly provided to the browser for access. The context utility provided by Microsoft implements dynamic encapsulation of the context in the method call. That is, we define a method that contains a response object. This method is called on that page (usually ASPX page) to obtain the context object set of the current request page accurately, very easy to use.

Then we change the code to the following:


Public   Class Accounts
{
Public Accounts ()
{
Httpcontext. Current. response. Write ( " Some text! " );
}
}

Now everything is normal. Here the httpcontext is under the system. Web namespace. Through httpcontext. Current, we can also point out the following common objects or classes:

Httpcontext. Current. Response
Httpcontext. Current. Request
Httpcontext. Current. Server
Httpcontext. Current. Request. Cookies

It also contains the following objects: application, applicationinstance, cache, error, items, trace, and user. You can refer to msdn and test the specific usage method.

After reading this, I believe you should suddenly understand and know how to do it. The following is a demo I made. There are 3 CS files in total. The code is very simple and you can see it at a Glance. the source code of the instance will be downloaded later.

1. app_code/accounts. CS
///   <Summary>
/// User logon and logout
///   </Summary>
Public   Class Accounts
{
Public Accounts ()
{
//
// Todo: add the constructor logic here
//
}
Public   Static   Void Checklogin ()
{
If (Httpcontext. Current. session [ " Username " ] =   Null )
Httpcontext. Current. response. Redirect ( " Login. aspx " );
}
Public   Static   Void Checklogin ( String Gourl)
{
If (Httpcontext. Current. session [ " Username " ] =   Null )
Httpcontext. Current. response. Redirect (gourl );
}
Public   Static   Void Login ( String Username, String Password)
{
If (Username =   " Admin "   && Password =   " Admin888 " )
{
Httpcontext. Current. session [ " Username " ] = Username;
Httpcontext. Current. response. Redirect ( " Default. aspx " );
}
Else
{
Httpcontext. Current. response. Redirect ( " Login. aspx " );
}
}
Public   Static   Void Logout ()
{
Httpcontext. Current. session [ " Username " ] =   Null ;
Httpcontext. Current. response. Redirect ( " Default. aspx " );
}
Public   Static   Void Logout ( String Gourl)
{
Httpcontext. Current. session [ " Username " ] =   Null ;
Httpcontext. Current. response. Redirect (gourl );
}
} 2. default. aspx. CS
Protected   Void Page_load ( Object Sender, eventargs E)
{
Accounts. checklogin ();
This . Username. Text = Session [ " Username " ]. Tostring ();
}
Protected   Void Logout_click ( Object Sender, eventargs E)
{
Accounts. logout ();
} 3. login. aspx. CS
Protected   Void Page_load ( Object Sender, eventargs E)
{
}
Protected   Void Btnlogin_click ( Object Sender, eventargs E)
{
Accounts. login ( This . Username. Text, This . Password. Text );
}

I have overloaded several methods under app_code/accounts. CS for flexible calling. The following code uses accounts when loading a page. checklogin (); first, check the user's logon status. Only after successful logon, the code after this method will have the opportunity to run, because the failure is directly transferred to the page.


Protected   Void Page_load ( Object Sender, eventargs E)
{
Accounts. checklogin ();
This . Username. Text = Session [ " Username " ]. Tostring ();
}

Source codeDownload:Httpcontext demo.rar

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.