Favorite asp.net basic learning notes and asp.net learning notes
It is worth sharing your favorite asp.net basic learning notes.
1. Overview browser-server B/S browsing
The interaction between the browser and the server forms the B/S mode of Internet access.
If HTML is uploaded to the server and sent to the server software (IIS), the server software can directly read the Static Page code and then return to the browser.
When ASPX communicates the files that the Server delivers to the server software (IIS) IIS and finds that it cannot process the aspx files, the ing table will find the response handler (isapi, Server Extension) based on the Suffix: how does IIS call extensible programs? A: The extensible program implements code based on the excuse provided by IIS, so IIS knows how to call it.
2. What is asp.net?
! Asp.net is a dynamic web page technology that runs. net code on the server, dynamically generates HTML, and then responds to browsers.
* Note: The main operations are server operations, and the browser only transmits commands.
! JavaScript can be used. Dom does a lot of work on the browser side, but a lot of work cannot be done on the browser side, such as data storage and database access without complicated business logic operations, logic operations with high security requirements.
! Server Control and HTML control generation relationship: Server control can be used on the aspx page to simplify opening. however, the browser only recognizes html. Therefore, when a page containing the server control is requested, the server control in the page is assembled into the corresponding HTML control code string, such as TextBox: <input type = "text"/>
! Asp.net: ASHX (general processing program) (the fastest running on the server), WebForm, WVC3 (Model, View, Controler)
! The server control is not a new control, and the html Tag is still generated on the browser side. Although the server control is easy to use, it also has some disadvantages. It is not good to use the server-side control.
3.common files in aspx.net (important)
The first small example: Dynamic login program
Public void ProcessRequest (HttpContext context) {string modelPath = context. server. mapPath ("LoginModel.htm"); string htmlSendBack = System. IO. file. readAllText (modelPath); context. response. contentType = "text/plain"; context. response. write (htmlSendBack); if (! String. isNullOrEmpty (context. request. form ["txtName"]) {if (context. request. form ["txtName"] = "zhu" & context. request. form ["txtPassword"] = "123") {context. response. write ("Logon successful! ");} Else context. Response. Write (" Logon Failed! ");}}
4. General Processing Program (HttpHandler)
1. General Processing Program (HttpHandler ):
Is a class that implements special interfaces of System. Web. IHttpHandler.
Any class that implements the IHttpHandler interface can be used as the target program for an external request: (any class that does not implement this interface will not be requested by the browser)
II. it is called and started to run by servers that support asp.net. an HttpHandler program is responsible for processing the access requests of one or more of its corresponding URLs, and receiving the access information sent by the client and generating the corresponding content.
3. You can create an HttpHandler program to generate the browser code and return it to the client browser.
4. The HttpHandler program can complete most tasks that can be completed by common class programs:
1. Data and URL parameters submitted by the lake client through the HTML Form
2. Create response information for the client
3. access the file system on the server
4. Connect to the database and develop database-based applications
5. Call other classes
5. Request the corresponding process
1. Enter http: // localhost: 80777/FirstShower. ashx in the address bar of the browser.
2. the server receives the user's request and finds it is a request. the ashx file is sent to the framework for execution, and fw finds the corresponding file first. ashx. After execution, the generated string (usually in html + css + javascript format) is returned to the browser.
3. the browser receives the data returned by the server and executes the data according to the http syntax, which is displayed on the interface to the user. (If html and other Code contain external files, send a separate request to the server for the corresponding file data again)
6. HTTP request details
7. ashx? -HttpHandler (General handler)
IHttpHandler hander = new page class ();
Hander. ProcessRequest (); // method in the called page class, which is an advantage of the interface
ContentType flag puts back the explanation language of the object on the webpage
Text/html translation in html
Is to set the ContentType attribute of the response message sent by the server. The browser uses different methods to process the [Response Message] according to this attribute.
8. compilation process
1. An HttpWorkerRequest and HttpApplication will be created for each request.
2. HttpWorkerRequest contains the data in each request message.
3. The HttpApplication object contains the code to be executed by each request.
4. Create a separate HttpApplication object for each request. All running processes for this request are completed in this object.
Factory understanding: HttpApplication pool. Every time HttpApplicationFectory is found in this pool, there are no idle HttpApplication objects. If there is any, it will be used out directly, and a new use will be created if there is no.
What the server does: accept browser requests, create page class objects, implement interfaces, call the methods in them, and return the corresponding stuff
In HttpRuntime, this class processes all requests and its work
1. Analyze the request message and encapsulate the packet data into an object called HttpWorkerRequest
2. Create an HttpContext object. The secondary object is the context of the current request, which contains all the parameter data for processing the request. The most important of these are HttpRequest and HttpResponse (convenient value)
3. HttpRequest mainly contains all the request information, which comes from the HttpWorkRequest object. The object contains the attributes: Form (Client connection data) QueryString (client url parameter)
4. HttpResponse mainly contains a FileStream object to save the data to be output to the browser during page class execution.
5. Call a static method of the HttpApplicationFectory class to create the corresponding attribute in the HttpApplication class object.
6. because the ProcessRequest method in the requested page class object is to be run in HttpApplication, you need to pass the HttpContext object to HttpApplication (IHttpHandler hander = creates the requested page class object through reflection )?
Execute the ProcessRequest method of HttpApplication (you can regard the execution process of this method as a pipeline). In this method, 19 delegate events are executed in sequence.
• Create the objects of the requested page class when there are 8th events
• The ProcessRequest method of the created page class is executed between 11 and 12.
9. How does the Server Accept and send data?
HTTP Request Response
9.1 Request (HttpRequest) & Response (HttpResponse)
I. browser data submission method
1 form (the data is stored in the Request Message format. txtname = jamws & txtpwd = 123)
<form action="login.ashx" method="post"> <input type="text" name="txtname"/> <input type="password" name="txtpwd"/> </form>
2 URL parameters in the address bar (the same as the Get method in the form): the key-Value Pair browser request attribute http: // 127.0.0.1/login. ashx? Txtname 1 = jordan & txtpwd 1 = 123
2. How does the server obtain the data submitted by the browser?
1. Obtain the Form data context. Request. Form ["txtname"]
2. Obtain the URL parameter: context. Request. QueryString ["txtname1"]
3. How does the server output parameters to the browser?
Context. Response. Write ("I am the data output from the server to the browser! ");
When you click the submit button in the browser, the browser automatically submits the value of the control with name in the form to the server by assigning a value to the string.
The Request itself can also be seen as all the parameters submitted by a client.
Request. Form contains only the data submitted by the client through post.
Reuqest. QueryString contains only the data submitted by the client in get mode.
Get: get, get -- when the browser sends a request message to obtain data from the server, it uses get
Post: Pass, locally, and send in the past. -- when the browser sends the request message to pass the parameter, it uses post
Public void ProcessRequest (HttpContext context) {context. response. contentType = "text/html"; System. text. stringBuilder sbHTML = new System. text. stringBuilder (); sbHTML. append ("
9.2 important Request (HttpRequest) members
The redirection principle is as follows:
------------------ Maintain data of the user control simulating wubForm ------------------
Public void ProcessRequest (HttpContext context) {context. response. contentType = "text/html"; string strNum1 = context. request. form ["txtNum1"]; string strNum2 = context. request. form ["txtNum2"]; // determines whether the format is correct string result = "0"; int num1 = 0, num2 = 0; if (! String. IsNullOrEmpty (strNum1 )&&! String. isNullOrEmpty (strNum2) {if (int. tryParse (strNum1, out num1) & int. tryParse (strNum2, out num2) {result = (num1 + num2 ). toString () ;}else {result = "incorrect input format" ;}} System. text. stringBuilder sbHTML = new System. text. stringBuilder (); sbHTML. append ("<! DOCTYPE>
------------------ Simulate the WebForm callback check mechanism ------------------
Public void ProcessRequest (HttpContext context) {context. response. contentType = "text/html"; string strNum1 = context. request. form ["txtNum1"]; string strNum2 = context. request. form ["txtNum2"]; // determines whether the format is correct string result = "0"; int num1 = 0, num2 = 0; // if the field contains hidden fields, if (! String. IsNullOrEmpty (context. Request. Form ["hidIsPostBack"]) {if (! String. IsNullOrEmpty (strNum1 )&&! String. isNullOrEmpty (strNum2) {if (int. tryParse (strNum1, out num1) & int. tryParse (strNum2, out num2) {result = (num1 + num2 ). toString () ;}else {result = "incorrect input format" ;}}} System. text. stringBuilder sbHTML = new System. text. stringBuilder (); sbHTML. append ("<! DOCTYPE>
-------------------------- Object-oriented calculator ----------------------------
// --------------------------------- Class definition samples // <summary> // a calculator class // </summary> public class Class1 {// The first operand public int num1 {get; set ;}// second operand public int num2 {get; set ;}// operator public string calculateChar {get; set ;}// result public string result {get; set;} public Class1 () {}/// <summary> /// calculation result /// </summary> /// <param name = "a"> first operand </param> // /<Param name = "B"> second operand </param> // <param name = "inline"> operator </param> public void GetResult (int, int B, string separator) {this. num1 = a; this. num2 = B; this. calculateChar = cursor; switch (this. calculateChar) {case "+": result = (num1 + num2 ). toString (); break; case "-": result = (num1-num2 ). toString (); break; case "*": result = (num1 * num2 ). toString (); break; case "/": result = (num1/num 2 ). toString (); break ;}}// response page class extends public class _ 07 CalculateFour: IHttpHandler {public void ProcessRequest (HttpContext context) {context. response. contentType = "text/html"; // instantiate a calculator object Class1 calcu = new Class1 (); string strNum1 = context. request. form ["txtNum1"]; string strNum2 = cont Ext. Request. Form ["txtNum2"]; string strOper = context. Request. Form ["optionOper"]; int num1 = 0; int num2 = 0; if (! String. IsNullOrEmpty (context. Request. Form ["hidIsPostBack"]) {// simulate a return visit if (! String. IsNullOrEmpty (strNum1 )&&! String. isNullOrEmpty (strNum2) {// if (int. tryParse (strNum1, out num1) & int. tryParse (strNum2, out num2) {// determine the format of calcu. getResult (num1, num2, strOper);} else {calcu. result = "Incorrect Parameter format" ;}} System. text. stringBuilder sbHTML = new System. text. stringBuilder (); sbHTML. append ("<! DOCTYPE>
10. Note that the content of the submitted Form
• Tags can only be input, textarea, or select tags. Only input (text box, CheckBox, etc.) users can enter values. <label>, <p>, <font> labels are only available for display and are not required to be submitted to the server.
• Only the value of the value attribute is submitted to the server. For example, the input tag has attributes such as title, type, and disabled, but these attributes are for display, users cannot modify the attribute. Only the value attribute is the property entered by users. Therefore, only the value of the value attribute is submitted to the server.
• The name attribute must be set for the tag. when learning Dom, we know that if you use Javascript to operate tags, you must set the id attribute for the tags. if you want to submit the value attribute of a tag to the server, you must set the name attribute for the tag. If you submit the attribute to the server, the key-value pair of "name = value" is submitted to the server, separated by &. Only a few tags, such as single-choice buttons, can be repeated. Other names cannot be repeated. name is used for the server, and id is used for the dom. For RadioButton, the same name is a group, and the value of the selected radiobutton is submitted to the server.
• If the disabled property of the control is set, the browser will not send the data to the server.
• Put It In the form label. Only the form label can be submitted to the server, and the input and other labels outside the form are ignored.
11. Use templates to clear code and use virtual template web pages
• Use hidden fields to simulate IsPostBack. <input type = "hidden" name = "hidIsPostBack" value = "true"/>
• In the template webpage, you can use placeholders when it comes to modifying the value. After that, you can simply replace them with {name }.
Public class _ 08Cal: IHttpHandler {public void ProcessRequest (HttpContext context) {context. response. contentType = "text/html"; // -------------------- read the html content template ---------------------- // obtain the physical path string path = context based on the virtual path. server. mapPath ("CalculateModel.htm"); // remember string strHTML = System. IO. file. readAllText (path); // remember here. // -------------------- obtain the content submitted by the browser ----------------------------- string str Num1 = context. request. form ["txtNum1"]; string strNum2 = context. request. form ["txtNum2"]; int num1 = 0; int num2 = 0; string result = ""; if (! String. IsNullOrEmpty (context. Request. Form ["hidIsPostBack"]) {if (! String. IsNullOrEmpty (strNum1 )&&! String. isNullOrEmpty (strNum2) {if (int. tryParse (strNum1, out num1) & int. tryParse (strNum2, out num2) {result = (num1 + num2 ). toString () ;}else {result = "incorrect input format" ;}}// ------------------------- output html to the browser ------------------------ // Replace the string and assign strHTML = strHTML. replace ("{num1}", num1.ToString ()). replace ("{num2}", num2.ToString ()). replace ("{result}", result. toString (); context. response. write (strHTML);} public bool IsReusable {get {return false ;}}}
// ----------------------------------- Template webpage display --------------------------------------- <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
12. Form submission methods: Get and Post
The amount of data transmitted by get is limited, and there is no limit on the amount of data sent by post.
Post will generate a refresh and resubmit form issue, and get will not have such a problem. You can solve this problem by retyping in the address bar.
Get URL data format ,? After & Split, URL encoding will be performed for Chinese characters in the URL
Only when the name is set in the form field will the parameter be passed. get can be seen clearly. If the submit has a value, it will also be passed.
13. Auto-increment data
• Http is stateless. Each time the browser requests a webpage class from the server, the server creates an object of this class, calls the method in the class for execution, and finally returns the output result to the browser, then the object is destroyed and the connection is closed.
• Browsers and servers do not know each other
Connection: Keep-Alive tells the server browser that the persistent Connection short Connection is equivalent to a one-time Connection.
Disadvantages of short connection: although the customer only wants to access a page, html still sends multiple new connection requests to the browser and establishes multiple new connections, consuming resources of both parties.
Http short connection: the server immediately closes the current connection (socket) after each response to a browser request ).
Persistent connection: the server does not immediately close the connection channel (socket) after sending data, but waits for a period of time (2 seconds) within two seconds, if the same browser sends a request again, continue to use this connection channel to output data to the browser, and then log on for a while until 2 seconds later, no new request is sent, then the server closes the connection Channel
The Decompilation tool keeps searching for, understanding the principle, and operating mechanism-Niu Ren
General handler _ simple request, get url parameters for ContentType (the essence of http parameter get/post transmission ). (Request Process principle; overall request process principle, page compilation process), request data, accept data, and orientation principle, simulate WebForm return check mechanism, and simulate wubForm user control data persistence, auto-increment (solves http stateless) and logon process
Next learning:
Addition calculator, addition, deletion, query, modification, construction of three-tier architecture, list and deletion, deletion of information, and Addition
Upload a single file and generate a thumbnail. Generally, the processing program outputs an image (simple verification code image), outputs the watermark image, downloads nearby, and reviews the summary.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.