Wednesday, April 1, 2009

httphandlers and asp.net

HTTPHANDLERS

They're a critical link in remoting, such as between a .Net application and a Flex presentation server. They can also be customized for other uses, such as dispatching synchronous or asynchronous data. Once you break the seal on httphandlers, you will likely discover they are profound in their utility.

This example demonstrates how to use HttpHandler technology using Asp.Net2.0 and C#.NET

This tutorial only uses the default namespace. The System namespace contains the EventHandler.

HttpHandler is a class that implements the IHttpHandler and IHttpAsyncHandler interfaces. This section describes how to create and register HttpHandler and provides examples of a synchronous handler, an asynchronous handler, and a handler factory.

First, you need to create a classlibrary HttpHandlerDLLVB. The code as follows:



using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace HttpHandlerDLL
{
public class MyHttpHander : IHttpHandler,IReadOnlySessionState
{
public bool IsReusable
{
get { return true; }
}

public void ProcessRequest(HttpContext context)
{
context.Response.Write("<h1><b>HttpHandler Test</b></h1>");
context.Session["Test"] = "Invoke a session in HttpHandler.";
context.Response.Write(context.Session["Test"]+"<br>");
context.Response.Write("<a href='HttpHandlerCsharp.aspx'><b>return</b></a>");
}
}
}

Then create a website project and reference classlibrary HttpHandlerDLLVB.dll.

At last configure your web.config as follows.

<httpHandlers>
<add verb="*" path="ViewHttpHandler.aspx" type="HttpHandlerDLLVB.HttpHandlerDLLVB.MyHttpHandlerVB,HttpHandlerDLLVB"/>
</httpHandlers>
The front end HttpHandlerVB.aspx page looks something like this:

<a href="ViewHttpHandler.aspx">Test HttpHandler</a></p>
</div>
The flow for the code behind page is as follows.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
}

No comments: