As we learned in the previous section, when creating a general processing program, the general processing program will implement the ihttphandler interface, so we can query it in the help document.. NET Framework 4 system. the interface in the Web namespace, we will find this interface, which has only one method and one attribute. The method is processrequest and the attribute is isreusable. the parameter of the processrequest method is the httpcontext object, it provides
Reference of internal server objects (such as request, response, session, and server) that provide services in HTTP requests, that is, six common objects.
After talking about this, there is no code, and there is always a sense of weakness, and everything is beyond the code, so the meaning of the expression is unclear. Let's look at the following code, this is our first ashx program and also a preliminary understanding.
Step: Open Visual Studio 2010, click File> New> website> Asp. net website and ASP. net can be empty websites. The language is selected in the upper left corner. I use C #.
Example 1:
Public class
Handler: ihttphandler {
Public
Voidprocessrequest (httpcontext context ){
Context. response. contenttype =
"Text/html ";
Context. response. Write ("<divstyle = 'background: # ff0000; font-size: 36px '> Hello World </div> ");
Context. response. write ("<Table border = '5px 'cellsapcing = '0'> <tr> <TD> helloasp. net </TD> </tr> <TD> helloc # </TD> </tr> </table> ");
Context. response. Write ("<a href = 'www .baidu.com '> Baidu homepage </a> ");
}
Public
Boolisreusable {
Get {
Returnfalse;
}
}
}
Here we changed contenttype to "text/html". When we right-click to view it in the browser, the browser parses the HTML code when the write method of the response object is output in the browser, writing code in this way is too limited. This is the first time you get started with it. It is important to understand your thoughts.
The response object is our first object to be exposed. Generally, the response object obtains data from the server and outputs the data to the browser. Then we can check the help document, what are the common methods for response objects?
There are many, but we can see that the write () method and the write () method have several overload functions. The write () function is used to write the parameter object to the HTTP Response output stream; there is also the clear () method, which is used to clear all the content output in the buffer stream; the close () method is used to close the socket link of the client; the end () method, the function is to send all the current buffered output to the client, stop the execution of the page, and trigger
Endrequest event. So many methods are introduced. How to use it. This section first comes here. The next section describes the request object.