The Hello, World HTTP Handler Example


The "Hello, World" HTTP Handler Example

Our first HTTP handler, HelloWorldHandler , demonstrates the basics of implementing a handler. This handler generates HTML markup that displays a "Hello" greeting in the Web browser. Listing 19-1 shows the code for this handler.

Listing 19-1 HelloWorld.ashx
 <%@WebHandlerLanguage="C#"Class="HelloWorldHandler"%> usingSystem; usingSystem.Web; publicclassHelloWorldHandler:IHttpHandler{ publicboolIsReusable{ get{returntrue;} } //Handlestherequestandgeneratestheresultingresponse. publicvoidProcessRequest(HttpContextcontext){ //Thestandardrequest/responseobjects. HttpRequestrequest=context.Request; HttpResponseresponse=context.Response; //Extractthenamefromtherequest. stringname=request.QueryString["name"]; //StartwritingouttheresponseHTML. response.ContentType="text/html"; response.Write("<html>"); response.Write("<head>"); response.Write("<title>HelloWorldHttpHandler</title>"); response.Write("</head>"); response.Write("<body>"); if((name!=null)&&(name.Length!=0)){ response.Write("<h1>Hello,"); response.Write(context.Server.HtmlEncode(name)); response.Write("!</h1>"); } else{ response.Write("Usage:<br>"); response.Write("HelloWorld.ashx?name=&lt;your_name&gt;"); } response.Write("</body>"); response.Write("</html>"); } } 

The HelloWorldHandler implements IHttpHandler and can be used as an HTTP handler in ASP.NET. HelloWorldHandler is implemented inside an .ashx file, HelloWorld.ashx. ASP.NET provides .ashx as a generic extension for implementing an HTTP handler without having to add any new configuration entries. The code in the .ashx file is dynamically compiled upon the first request made to it, by using the same dynamic compilation model that is used for an .aspx file as described in Chapter 2. Every .ashx file must contain a WebHandler directive, which is used to specify the class within file that provides the implementation of IHttpHandler . All requests to the HelloWorld.ashx file are processed by the handler implemented in this file. Figure 19-1 shows the results of making a request to this file.

Figure 19-1. The results of making a request to HelloWorld.ashx

graphics/f19hn01.jpg

The HelloWorldHandler class generates HTML markup into the response stream. Therefore, the handler sets the ContentType property of the response to "text/html" . An HTTP handler should always set the content type of the response it generates, unless the content type can be automatically inferred from the URL. With HelloWorldHandler , it is especially important to set the content type of the generated response. This is because the browser making the request cannot infer the content type from the request URL as it can with URLs that have .htm or .html extensions. The handler writes standard HTML markup into the response stream that gets rendered within the browser.

The handler retrieves the name parameter from the URL's query string (the text containing name/value pairs following the question mark in the URL) and uses its value as part of the greeting it generates. The name value is HTML encoded before it is written into the response stream. This prevents any malicious users from sending script within the name value, which is a common case of cross-site scripting. Taking this precaution ensures that the HTTP handler will not suffer from a lapse in security.

When the name parameter isn't specified, HelloWorldHandler generates HTML that describes its correct usage. Consider following a similar pattern in your HTTP handler implementations by handling missing or invalid parameters gracefully via an informative display that describes the correct usage of the handler when appropriate.



Developing Microsoft ASP. NET Server Controls and Components
Developing Microsoft ASP.NET Server Controls and Components (Pro-Developer)
ISBN: 0735615829
EAN: 2147483647
Year: 2005
Pages: 183

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net