ASP.NET Underground tour (Part 1)

To fully understand how to use ASP.NET is necessary to know the basic operation of the web (for example as occurs a request from browser to server, what are the technologies used, how a web server processes the request, etc. …).

Understanding why a web page is not interpreted correctly in a browser, because a request is not performed as expected, or generate the appropriate HTML code from a custom control, are questions that do not depend only on knowledge of ASP.NET (or any other technology), but also from the knowledge of how a web application works.

HTTP Request and Response

The HTTP protocol (HyperText Transfer Protocol) was created as a mechanism for transferring hypertext documents defined as HyperText. The hypertext documents are related to each other by keywords clickable or hypertext links (hyperlinks).

Currently, however, a hypertext has become more complex, and the links are no longer just text, but can also be images; hypertext documents have become multimedia documents as well as the Protocol has been updated, also to compensate for inefficiencies of the first version, including the lack of security mechanisms.

It works, however, is always the same: an answer in response to a request (client / server); the client (browser) makes a request for a resource by opening a connection to the server (web server) which responds by sending the resource and the connection is closed.
A HTTP request is by commands established by the Protocol, including GET, POST, HEAD. Here’s an example:

We recall a page from the browser: http://localhost:80/Hello.html
Here is the HTTP request the browser sends to the server “localhost”:

GET /Hello.html HTTP/1.1
Host: localhost:80
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: it-it,it;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip, deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection: keep-alive

What interests us most is the first line (which specifies, by the GET command, the resource request, the protocol / version used) and the second line (which specifies the server to which the request is made). Everything else is superfluous for our purpose.

The server receives the request and processes it: in this case the server will look for the resource indicated in the request and send the Response to the client. Here’s how the Response is made:

HTTP/1.1 200 OK
Content-Type: text/html
Last-Modified: Wed, 06 Jul 2011 15:53:21 GMT
Accept-Ranges: bytes
Etag: "9259e2d4f43bcc1:0"
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Sat, 15 Oct 2011 16:28:52 GMT
Content-Length: 130
<html>
    <body>
        <h1> Hello World </h1>
    </body>
</html>

The browser receives the response and shows the resource. In this case it is a simple HTML page, then the browser will render the HTML code to make it visible. Operation is very basic, so needless to say any more.

Who respond to this request?

The server.
Well, now we need to explain …
In ancient times, there where … … CGI applications.
no, maybe I left too far!

We must speak of the present, and especially of the Microsoft platform. The solution is a service (daemon process) that listens on port 80 (default) and processes the requests. We are talking about IIS (Internet Information Services).

How does IIS to respond at the requests? Configuration is done by a mapping between file extensions and IIS extensions, which are typically external DLL calls ISAPI DLLs (Internet Services Application Programming Interface).

When a request comes to the server, it examines the file extension, the request can be handled directly by IIS (such as a JPG or HTML or any other static file is read and sent directly to the Response), or addressed to a particular extension ISAPI. This is the .Aspx file case.

ISAPI mapping

ISAPI mapping

Requests on .Aspx file are processed by aspnet_isapi.dll extension so that, in turn, send again the execution from the ASP.NET engine of the framework to the APS.NET’s HTTP pipeline. Like the configuration of IIS, the ASP.NET engine map HTTP requests with specific handlers. These operators are just “pieces of code” that are concerned with generating the HTML code to be sent back in response.

The configuration of these mechanisms can be found in the web.config in the folder of the framework in use:
$WINDOWS$\Microsoft.NET\Framework\$VERSION$\CONFIG
Below is shows the default configuration for the handler:

...
<httpHandlers>
...
...
<add path="*.svc" verb="*" type="System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" validate="false"/>
<add path="trace.axd" verb="*" type="System.Web.Handlers.TraceHandler" validate="True"/>
<add path="WebResource.axd" verb="GET" type="System.Web.Handlers.AssemblyResourceLoader" validate="True"/>
<add path="*.axd" verb="*" type="System.Web.HttpNotFoundHandler" validate="True"/>
<add path="*.aspx" verb="*" type="System.Web.UI.PageHandlerFactory" validate="True"/>
<add path="*.ashx" verb="*" type="System.Web.UI.SimpleHandlerFactory" validate="True"/>
<add path="*.asmx" verb="*" type="System.Web.Services.Protocols.WebServiceHandlerFactory, System.Web.Services, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" validate="False"/>
...
...
 </httpHandlers>
...
Pipeline ASP.NET

Pipeline ASP.NET

As you can see, for the .Aspx pages is called “PageHandlerFactory” that will search the compiled class and invoke the requested page. In the event that the compiled class did not exist, the framework compiles itself the page and its code.

Riferimenti:

http://www.4guysfromrolla.com/articles/011404-1.aspx
http://www.15seconds.com/issue/020417.htm
http://msdn.microsoft.com/en-us/library/aa479328.aspx

Tag di Technorati: ,,,,,,,,,,,,

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s