Amore typical approach to handling these requests is to use an HTTP handler. Ahandler has the ability to
respond to requests with more direct control over the response data. Essentially, an HTTP handler exists
very early in the ASP.NET processing pipeline and can deal directly with requests without having to worry
about HTML data being generated by the page and the unnecessary burden of the entire page lifecycle.
Additionally, the handler need not be concerned with having the initial request be a properly formed page
but can instead have it be a custom formatted message that has meaning within the context of your application.
Often, this is an XML document used to transfer data between the client and the server.
Using a HTTP Handler
To demonstrate the use of a HTTP handler to accept arguments sent from an XMLHttpRequest object,
you first define the user interface within the web page, as shown in the following code. This simply
shows a drop-down list with a list of three customers. When one of the customers is selected, that customer
ID is used as part of the asynchronous request to request a specific set of data pertaining to that
customer’s details only:
You also provide an implementation of the JavaScript function named LoadCustomer() referenced in
the onchange attribute of the select element as follows:
function LoadCustomers()
{
var xmlHttpObj;
xmlHttpObj= CreateXmlHttpRequestObject();
if (xmlHttpObj)
{
xmlHttpObj.open("GET","http://" + location.host + "/Test/DataFile.xml", false);
xmlHttpObj.send(null)
// If the request was ok (ie. equal to a Http Status code of 200)
if (xmlHttpObj.status == 200)
{
var xmlDoc = xmlHttpObj.responseXML;
// Our list of nodes selected using the X Path argument
//var nodes = xmlDoc.selectNodes(“//Customers/Customer”);
var nodes = xmlDoc.selectNodes("//Customers/Customer/Lastname/text()");
// Obtain a reference to the select drop list control.
var ctrl = document.getElementById("ddlCustomers");
for (var i=0; i less then nodes.length; i++)
{
// Get the lastname element from our XML data document
var lastName = nodes[i].nodeValue;
// Create a new node.
var htmlCode = document.createElement('option');
// Add the new node to our drop list
ctrl.options.add(htmlCode);
// Set the display text and value;
htmlCode.text = lastName;
htmlCode.value = lastName;
}
}
else
{
alert('There was a problem accessing the Customer data on the server.!');
}
}
}
Finally, the implementation of the server-side HTTP handler itself is shown in the following code block.
This is the handler that receives the asynchronous call from your XMLHttpRequest object, extracts the
customer number or ID, and then uses that to return an XML document containing only that customer’s
name and email address. This code resides within the AsyncRequestHandler.ashx file.
using System;
using System.Web;
public class AsyncRequestHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
// Grab the URL parameters
string param = context.Request.QueryString["arg"];
const string xmlData = @"
string returnXML = null;
switch (param)
{
case "1":
returnXML = string.Format(xmlData, "Big Bob", "big@bob.com");
break;
case "2":
returnXML = string.Format(xmlData, "Small Sammy","small@sammy.com");
break;
case "3":
returnXML = string.Format(xmlData, "Large Larry","large@larry.com");
break;
}
context.Response.ContentType = "application/xml";
context.Response.Write(returnXML);
}
public bool IsReusable {
get {
return false;
}
}
}
No comments:
Post a Comment