At DevConnections, I presented a session entitled "The Atlas Framework--The Big Picture" where I first explained the background of the Ajax lifestyle and the motivation for Atlas. I then finished off presenting a couple of examples--how to call a Web service from an Atlas page and how to invoke a page method.
It is interesting to see how page methods work internally.
A page method is an inline method decorated with the [WebMethod] attribute and placed inside a server-side script block of type C#/VB.
<script type="text/C#" runat="server">
[WebMethod]
public string RetrieveMoreInfo(string id)
{
return RetrieveMoreInfoInternal(id);
}
</script>
To invoke this method from the client, you need the following Javascript code:
function GetMoreInfo()
{
PageMethods.RetrieveMoreInfo(
document.getElementById('cboEmployees').value,
OnComplete,
null);
return false;
}
What's the PageMethods object? And who created that? PageMethods is a Javascript object declared in the HTML source. The Atlas server-side framework provides for the following code. To see this code, you just open the View Source window of your browser when the Atlas page is displayed.
<script type="text/javascript">
var PageMethods = new function() {
var cm=Web.Net.PageMethodRequest.createProxyMethod;
cm(this,"RetrieveMoreInfo","id");
}
</script>
When the user triggers the above GetMoreInfo Javascript function, the HTTP request contains a couple of extra input fields added by proxy method. They are __serviceMethodName and __serviceMethodParams. Here's a typical request:
POST /Atlas/Postback.aspx HTTP/1.1
Cache-Control: no-cache
Connection: Keep-Alive
Content-Length: 478
Content-Type: application/x-www-form-urlencoded
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-us,it;q=0.5
Cookie: .ASPXANONYMOUS=M ... s1
Host: localhost:2471
Referer: http://localhost:2471/Atlas/Postback.aspx
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; Avalon 6.0.5070; WinFX RunTime 3.0.50727)
UA-CPU: x86
__serviceMethodName=RetrieveMoreInfo&
__serviceMethodParams={"id":"5"}&
__VIEWSTATE=/wEPDw ... EtV
This request is intercepted by an Atlas-specific HTTP module, named ScriptModule. The HTTP module kicks in immediately after the handler for the request has been found. If this handler is a Page class--that is, the request is directed to a page (as opposed to a Web service)--the HTTP module registers its own handler for the PreRenderComplete event.
In turn, the PreRenderComplete event code registers a render method delegate (a custom piece of code that runs during the Render method). This code retrieves the method name and parameters, does any due deserialization, and invokes the page method via reflection.
Everything else works just as usual.
NOTE: These details may change in future builds of Atlas