HttpHandlers-configuration

HttpHandlers-configuration


 
   I assume here you are familiar with HttpModules and HttpHandlers.

This article will help you to create API using .Net.

1. Create classs library project say MyProject.Core
2. Add referance of System.Web
3. Create on class called xyzHandler and inherit it from IhttpHandler

E.g. 

 

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

namespace MyProject.Core
{
    public class xyzHandler:IHttpHandler
    {

        #region IHttpHandler Members

        public bool IsReusable
        {

            get { return true; }

        }

        public void ProcessRequest(HttpContext context)
        {

            if (!string.IsNullOrEmpty(context.Request.QueryString["UserName"]))
            {
                string username = context.Request.QueryString["UserName"];

                context.Response.Write("Hi" + username);
            }

        }

        #endregion


    }
}


This handler will collect the username parameter posted by HTTP get method.
If you use Request.Parms, handler can accept parameters posted by both HTTP GET/POST method.
Now, you can add referance of MyProject.Core dll into your web and configure your web application to use this handler.

You need to add below httphandler element under System.Web node in your web.config. Your web.config will look like below one

<system.web>
<compilation debug="true"/>

<authentication mode="Windows"/>

<httpHandlers>

<add verb="GET" path="xyz.ashx" type="MyProject.Core.xyzHandler, MyProject.Core"/> </httpHandlers>

</system.web>


In above configuration you can use

1. verb="*" it indicates handler can accept all HTTP methods like GET,POST,OPTION etc.

2. path="*.ashx" this indicates any request coming to extension .ashx will be handled by xyzHandler

3. For type you first need to give fully qualified name of class like MyProject.Core.xyzHandler and name of assembly like MyProject.Core.

Below url will let you capture the field username posted by HTTP GET method.

TEST URL
http://localhost:17499/FastFileDownLoadDemo/xyz.ashx?username=satalaj







Currently rated 2.5 by 4 people

  • Currently 2.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5