In this article I will explain with an example, how to call (execute) Generic HTTP Handler from Code Behind using C# and VB.Net in ASP.Net.
 
 
The Generic Handler
The following Generic HTTP Handler accepts a QueryString parameter “name” and returns string greeting message.
<%@ WebHandler Language="C#" Class="Handler" %>
 
using System;
using System.Web;
 
public class Handler : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        string name = context.Request.QueryString["name"];
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello " + name);
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }
}
 
 
Namespaces
You will need to import the following namespace.
C#
using System.Net;
 
VB.Net
Imports System.Net
 
 
Call (Execute) Generic HTTP Handler from Code Behind
Inside the Page Load event, the Absolute URL i.e. Complete URL of the Generic Handler is saved in a String variable.
This variable is then passed as parameter to the DownloadString method of the WebClient class object which internally calls (executes) the Generic HTTP Handler and returns the response received.
Note: The URL used in below code snippet consists of Port Number which needs to be changed to the Port Number assigned to your project by Visual Studio.
 
C#
protected void Page_Load(object sender, EventArgs e)
{
    string handlerUrl = "http://localhost:6340/Call_Generic_Handler_CodeBehind/Handler.ashx?name=Mudassar";
    string response = (new WebClient()).DownloadString(handlerUrl);
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
    Dim handlerUrl As String = "http://localhost:6340/Call_Generic_Handler_CodeBehind/Handler.ashx?name=Mudassar"
    Dim response As String = (New WebClient()).DownloadString(handlerUrl)
End Sub
 
 
Screenshot
ASP.Net: Call (Execute) Generic HTTP Handler from Code Behind using C# and VB.Net
 
 
Demo
 
 
Downloads