Delphi 2005 does not provide support for creating ASP.NET HttpHandlers out-of-the-box, but it is not so difficult to create one:-)
The HttpHandler is a class that implements IHttpHandler interface, which contains only two members:
"IsReusable" property that tells HttpRuntime if it is OK to pool, and the core method "ProcessRequest", which accepts "HttpContext" param.
From the perspective of the ASP.NET HttpRuntime it does not matter if an incoming request to process is for an ASP.NET web page, an ASP.NET web service or for an HttpHandler. The only thing that matters is presence of a “ProcessRequest” method, which does all the work. A class that implements IHttpHandler interface is supposed to write some content to a "Response" object during "ProcessRequest" call. In the case of a web page this would be an HTML, for web service it would be an XML SOAP packet, but in general it could be anything, for example an image. HttpHandlers are useful because they do not need to inherit from anything, so they do not bring any overhead.
I have just converted an MSDN example of a simple HttpHandler implementation to Delphi for .NET.
Steps:
1. New | ASP.NET Web Application - Delphi for .NET. Save All. You may want to save this project as “WebAppHttpHandlerDemo“.
2. Add an empty unit to a project. Save As "HttpHandler1.pas". Copy and paste this code replacing the original content of the unit.
unit HttpHandler1;
interface
uses
System.Web;
type
THttpHandler1 = class(System.&Object, IHttpHandler)
public
function get_IsReusable: boolean;
public
procedure ProcessRequest(context: HttpContext);
property IsReusable: boolean read get_IsReusable;
end;
implementation
{ THttpHandler1 }
function THttpHandler1.get_IsReusable: boolean;
begin
Result := True;
end;
procedure THttpHandler1.ProcessRequest(context: HttpContext);
begin
context.Response.Write(’<H1>HttpHandler demo</H1>’);
context.Response.Write(’<p>Your browser:</p>’);
context.Response.Write(’Type: ‘ + context.Request.Browser.&Type + ‘<BR>’);
context.Response.Write(’Version: ‘ + context.Request.Browser.Version);
end;
end.
3. Add to project’s "web.config" file the following entry inside "system.web" element.
<httpHandlers>
<add verb="*" path="HttpHandler1.aspx" type="HttpHandler1.THttpHandler1, WebAppHttpHandlerDemo" />
</httpHandlers>
This assumes that the project name is "WebAppHttpHandlerDemo". Note that "HttpHandler1.aspx" file does not exist and does not have to. The first part of a "type" attribute is fully qualified class name of a class that implements a IHttpHandler interface, and the second part is the name of the assembly to load this type from.
4. Put a button on "WebForm1.aspx" (which is our web app default page).
5. Double-click the button and add the following code:
procedure TWebForm1.Button1_Click(sender: System.Object; e: System.EventArgs);
begin
Response.Redirect(’HttpHandler1.aspx’);
end;
6. Run.
Very good reference about ASP.NET processing can be found in the MSDN Magazine article “HTTP Pipelines“.
{ 3 } Comments
There is also Async http handler nterface that can be used to process http requests asynchroniusly.
It is a verry powerfull tool. It is used by Intraweb for example for ISAPI .NET modules.
You may want to look at Scott Hanselman’s boilerplate http handler http://www.hanselman.com/blog/PermaLink,guid,5c59d662-b250-4eb2-96e4-f274295bd52e.aspx although it’s C#, should be easy enough to port to Delphi.
Phil Haack has created an An Abstract Boilerplate HttpHandler (again C#) here http://haacked.com/archive/2005/03/17/2394.aspx
Feeding Phil’s code through the BabelCode client, gives something that looks nearly work-able.
very good, the webform1 can access the httphandle1 interface just as it have a satement in the web.config, but i still doult about the function of web.config and it’s verb gramma.
Post a Comment