Tuesday, December 29, 2009

Generic Handler

Overview

An ASP.NET HTTP handler is the process that runs in response to a request made to an ASP.NET Web application. The most common handler is an ASP.NET page handler that processes .aspx files. When users request an .aspx file, the request is processed by the page through the page handler. You can create your own HTTP handlers that render custom output to the browser.

MSDN Link

For detail understanding of the topic you can use msdn link i.e. http://msdn.microsoft.com/en-us/library/bb398986.aspx

Example

Now let’s take one practical example of generic handler so that we can understand in better way.

Functional requirement – lets say developer wants to push user email address in to some database using client side form post, but before posting the form developer also want to send an email on that email address. To implement this functionality there are few alternate options but lets go with generic handler.

1) Aspx page

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<html>
<head>
<script language="javascript">
function createXMLHttpRequest() {
try { return new XMLHttpRequest(); } catch (e) { }
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { }
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { }
return null;
}
function SendEmail() {
var xmlHttpReq = createXMLHttpRequest();
xmlHttpReq.open("GET", "Handler.ashx?email=" + document.getElementById('email').value, false);
xmlHttpReq.send(null);
var yourJSString = xmlHttpReq.responseText;
}
</script>
<title>Untitled Page</title>
</head>
<body>
<form action="https://www.xyz.com/" method="post" >

<input id="email" maxlength="100" name="email" size="50" type="text" />

<input type="button" onclick="SendEmail()" value="submit"/>
</form>
</body>
</html>

2) Handler.ashx

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler {

public void ProcessRequest(HttpContext context)
{

System.Web.Mail.MailMessage msgMail = new System.Web.Mail.MailMessage();
msgMail.To = context.Request.QueryString["email"].ToString();
msgMail.From = "nishant@zzz.com";
msgMail.Subject = "Hey";
msgMail.BodyFormat = System.Web.Mail.MailFormat.Html;
string strBody = "<html><body>hey ... <br>" +
" <font color=\"red\">how are you..? </font></body></html>";
msgMail.Body = strBody;
System.Web.Mail.SmtpMail.SmtpServer = "yyy.123.abc";
System.Web.Mail.SmtpMail.Send(msgMail);
context.Response.Write("done");
}

public bool IsReusable {
get {
return false;
}
}

}

Above two steps is all about implementing generic handler. In the first step we have just call handler file by passing querystring to it. i.e. “SendEmail()” function in aspx page. In the second step we have added a generic handler by adding new file to the application. Once we added new generic handler file, it will automatically implement ‘IHttpHandler’ interface and will be having empty implementation of ‘ProcessRequest’ and ‘IsReusable’ methods. Now once we call the handler file from javascript it will sent the control to ‘ProcessRequest’ method of the handler class. And as we can see in above example we are suppose to write our cutome logic in ‘ProcessRequest’ method.

That’s all isn’t it easy to implement handler and not only its easy way, it has only one event unlike the page so it is faster then page level request execution, and thus it also improves the performacne :)