Aspx, ascx, and ashx usage Summary

Source: Internet
Author: User

Asp.net development is familiar with. aspx,. ascx, And. ashx. There are many articles about them on the Internet. "I have to get a glimpse of it on the paper, and I know that this is the case." I will take a note here.

1. aspx
Web form design page. A Web form page consists of two parts: visual elements (html, server control, and static text) and the programming logic of the page (the design view and Code view in VS can see their corresponding files respectively ). VS stores these two components in a separate file. The visual element is created in the. aspx file.

2. ascx
Asp.net user control is developed as a Web page that encapsulates specific functions and behaviors (the two are used on various pages of Web applications. A user control contains a combination of html, code, and other Web or user controls, and is saved in its own file format on the Web server. Its extension is *. ascx. The default configuration in asp.net does not allow Web clients to access these files through URLs, but other pages of this website can integrate the functions contained in these files.

3.. ashx
The first two are too familiar. This is the focus.

(1) Example
The. ashx file is mainly used to write web handler. Using. ashx allows you to focus on programming without having to worry about related web technologies. Well known. aspx is used to parse the html Control tree ,. all html contained in aspx is actually a class, and all html is a member in the class. This process is. ashx is not required. Ashx must contain the IsReusable attribute (this attribute indicates whether it can be reused, usually true). To use Session in the ashx file, you must implement the IRequiresSessionState interface.
A simple example of modifying the password of a logon User:

Copy codeThe Code is as follows:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. Web. SessionState;

Namespace Test
{

Public class HandlerTest: IHttpHandler, IRequiresSessionState
{

Public void ProcessRequest (HttpContext context)
{
Context. Response. ClearContent ();
Context. Response. ContentType = "text/plain ";
Context. Response. Cache. SetCacheability (HttpCacheability. NoCache); // No Cache

String action = context. Request. Params ["action"]; // external Request
If (action = "modifyPwd") // change the password
{
String oldPwd = context. Request. Params ["pwd"];

// The IRequiresSessionState interface must be implemented when Session is used in the ashx file.
// Session ["LogedUser"] indicates the logon user Session. The user name and password are both test
If (oldPwd. ToUpper ()! = (Context. Session ["LogedUser"]) as Customer). Password. ToUpper () // The old Password entered by the user is different from the current Login User
{
Context. Response. Write ("Incorrect old password! ");
}
Else
{
Context. Response. Write ("the old password is entered correctly! ");
}
}


Context. Response. End ();
}

Public bool IsReusable
{
Get
{
Return true;
}
}
}
}

Client call (js and page ):
Copy codeThe Code is as follows:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeBehind = "ASHXTest. aspx. cs" Inherits = "ASHXTest" %>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> mytest </title>
<Script type = "text/javascript">
Function $ (s) {if (document. getElementById) {return eval ('document. getElementById ("'+ s +'") ');} else {return eval ('document. all. '+ s );}}

Function createXMLHTTP (){
Var xmlHttp = false;
Var arrSignatures = ["MSXML2.XMLHTTP. 5.0", "MSXML2.XMLHTTP. 4.0 ",
"MSXML2.XMLHTTP. 3.0", "MSXML2.XMLHTTP ",
"Microsoft. XMLHTTP"];
For (var I = 0; I <arrSignatures. length; I ++ ){
Try {
XmlHttp = new ActiveXObject (arrSignatures [I]);
Return xmlHttp;
}
Catch (oError ){
XmlHttp = false; // ignore
}
}
// Throw new Error ("MSXML is not installed on your system .");
If (! XmlHttp & typeof XMLHttpRequest! = 'Undefined '){
XmlHttp = new XMLHttpRequest ();
}
Return xmlHttp;
}

Var xmlReq = createXMLHTTP ();

// Send an ajax request (here we will verify the validity of the old password)
Function validateOldPwd (oTxt ){
Var url = "/HandlerTest. ashx? Action = modifyPwd & pwd = "+ escape (oTxt. value); //. ashx File
XmlReq. open ("get", url, true );
XmlReq. setRequestHeader ("If-Modified-Since", "0 ");
XmlReq. onreadystatechange = callBack;
XmlReq. send (url); // send text
}

Function callBack (){
If (xmlReq. readyState = 4 ){
If (xmlReq. status = 200 ){
Alert (xmlReq. responseText); // receives text
}
Else if (xmlReq. status = 404 ){
Alert ("Requested URL is not found .");
} Else if (xmlReq. status = 403 ){
Alert ("Access denied .");
} Else
Alert ("status is" + xmlReq. status );
}
}

</Script>

</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Input id = "txtOldPwd" type = "text" onblur = "validateOldPwd (this)"/>
</Div>
</Form>
</Body>
</Html>

Analysis:
A. Previously we used to implement functions through a simple aspx file. In fact, we can also use ashx.
The author once wrote an ajax: Introduction to data transmission methods. Through comparison, we found that aspx should separate the display and processing logic in the front and back, so we made two files. In fact, in the final compilation, aspx and cs will still be compiled into the same class. in this case, we need to design some html logic processing. Unlike ashx, it simply returns the expected results directly to the web http request. html processing is less than aspx (but ashx can also process some html logic, but it is generally not used ). In theory, ashx is faster than aspx.

B. In the same old document, we know several data transmission methods. In fact, ashx can be implemented (modify context. Response. ContentType in the ashx file). I will not go into details here.

(2) ashx is particularly suitable for generating dynamic images and dynamic text (such as plain text, json, xml, and javascript.

(3) The. ashx file has a disadvantage: It is very troublesome to process the control's sending back events. Processing data sending back usually requires some. aspx page functions. You can only manually process these functions (rather than directly creating An aspx file for processing ). Therefore, you can use. ashx to output projects that do not require sending back.

4. Summary
Aspx --> P (Page)
Ascx --> C (Control)
Ashx --> H (HttpHandler)

When the browser accesses the web server, we finally receive html text. The browser interprets these labels through the rendering engine to display visible effects on the screen. Asp.net, however, is a platform technology we use to interpret html in disguise. To put it bluntly, it aims to improve productivity and has many technical terms, in essence, it is something in the html field (if you do not use dynamic page technologies to fully utilize html and browsers (including js of course) to achieve dynamic page effects, then you will find that the effect has a considerable amount of code ). therefore, the bottom layer of web development is a bunch of html tags. Both asp.net and jsp are html packaging methods and are the product of html.

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.