ArticleDirectory
- Example of logic processing:
1. web applications and websites 2. The simplest ASP. NET program processing process 3. ASP. NET ispostback secrets
1. Web ApplicationsProgramAnd website |
Webapplication (Web application) and website (website) are different. website exists to be compatible with the habits of developers who transfer from ASP. It is easy to use, for example, you do not need to create namespaces or CSCodeAfter modification, you do not need to restart to view the changes (whether it is website or webapplication, you do not need to restart to modify Aspx. After each webapplication modification, click [generate solution] to immediately see the modification effect), but it is not conducive to engineering development. For example, code errors are not easy to discover, and the code is not named. There is no difference in development technology, but the development and debugging habits are different. To facilitate development, you do not need to set the start page for each debugging. In the options of the project, choose web> Start Operation> current page. In this way, the current active page is the start page. LectureUse website for simple basic knowledgeAdvanced technologies andUse webapplication for Project Creation.
Create webapplication
Create a website
2. The simplest ASP. NET program processing process |
The "name" element must be added to the form Element submitted to the server.
Each time a user requests to access the ashx page, the processrequest method is called to obtain the Request Parameters of a visitor by accessing context. Request. In this example, the username parameter is obtained.
In processrequest, context. response is used to send data back to the browser. At the end of processrequest, when the server completes the service for this visitor, the browser submits data to the server, and the data submitted forms (input, select, textarea, etc.) are placed in the form, in form, the action attribute is used to set the page to which the form is submitted. In order to retrieve the value of the form item on the server, the name attribute must be set for the form element in HTML,Note that the ID is used to operate the DOM in Javascript, And the name is submitted to the server.. Use context. request ["username"] on the server to obtain the submitted attribute value based on the name of the form item. Use context. response. Write to output the HTML content after processing to the browser.
Create an ashx file and write it in processrequest
Context. response. contenttype ="Text/html";//HTML indicates that the browser is recognized as an HTML languageStringUsername = context. request ["Username"];//Obtain the value of the form submitted by the user whose name is username.Context. response. Write (username + Hello );
Create an HTML page, create a form, and set the action to point to the newly written ashx file. When you click Submit, all parameters in the form will be passed to the hello1.ashx file.
form action =" hello1.ashx " > name input type =" text " name =" username " /> input type =" Submit " value =" Submit " /> form >
In this case, start the browser to jump from HTML to the ashx page, and the previous user name and input box will be unavailable. There is no good solution to this problem, I had to draw the previous image in the ashx file. See the program.
Public Void Processrequest (httpcontext context) {context. response. contenttype = " Text/html " ; String Username = context. request [ " Username " ]; Context. response. Write ( @" <Form action = 'hello1. ashx '> name: <input name = 'username' type = 'text'/> <input value = 'Submit' type = 'submit'/> </form> " ); // This operation draws the previous HTML and writes the previous HTML code to context. response. write (@ means that multiple lines of text are not recognized separately) // Context. response. Write ("Hello World "); Context. response. Write (username );}
You can also useContext. server. mappathBuilt-in read
Context. response. contenttype = " Text/html " ; String Username = context. request [ " Username " ]; String Path = context. server. mappath ( " Hello2.html " ); // The full path of hello2.htm is obtained, so the mappath method is used. String Content = system. Io. file. readalltext (PATH );// Read files Context. response. Write (content ); // Context. response. Write (PATH ); Context. response. Write (username );
In this way, you do not need to copy all the HTM code, and do not need to open HTM first, and then jump to ashx. Now you can open the ashx file directly to display all the content on the HTM page.
3. ASP. NET's ispostback secret |
When you enter hello2.ashx, the content is directly output to the browser. The user fills in the value in the output content, and then click Submit. The server will know that "submitted back" (PostBack)
Add a hidden field to form to distinguish whether to directly enter the page for the first time or re-enter ashx after clicking submit:<Input type = "hidden" name = "ispostback" value = "true"/>If ispostback = true can be read from the request, it means to click submit and re-enter ashx; otherwise, it is the first time to access ashx. Ispostback is a flag.
<Input type = "hidden" name = "ispostback" value = "true"/>
StringIspostback = context. request ["Ispostback"];If(Ispostback ="True") {Context. response. Write ("Enter");}Else{Context. response. Write ("Go directly");}
HTTP is a model of request and response. The server does not read the web page of the browser. What is returned is the data submitted by the client web page. Example of logic processing:
First write the placeholder @ vlaue, @ MSG, and then ashx (@ value can also write $ value, defined by yourself, consistent with the line)
Public Void Processrequest (httpcontext context) {context. response. contenttype = " Text/html " ; String Username = context. request [ " Username " ]; String MSG = "" ; // Declare MSG variable String Ispostback = context. request [ " Ispostback " ]; If (Ispostback = " True " ) {Context. response. Write ( " Enter " ); Msg = Username + " Hello! " ;} Else {Context. response. Write ( " Go directly " ); Msg = Username + "" ;} String Path = context. server. mappath ( " Hello2.html " ); // The full path of hello2.htm is obtained, so the mappath method is used. String Content = system. Io. file. readalltext (PATH ); // Read files Content = content. Replace ( " @ Value " , Username ); // Replace placeholder @ Value Content = content. Replace ( " @ Msg " , MSG); context. response. Write (content );}
Before submission
After submission
All forms are submitted with name as the key and value as the content. Other attributes are not submitted to the server.