8

I have created a http handler fro my Jquery ajax call. which is working fine the jquery call is mentioned below

 $.ajax({
    url: "Services/name.ashx",
    contentType: "text/plain",
    data: {
        CustMobile: a,
        CustName: b,
        CustEmail: c
    },
    contentType: "application/json; charset=utf-8",
    success: function (data) {
        $("#loading").hide();
    },
    error: function () {
        $("#loading").hide();
    }
});

Now my problem is this can I call the name.ashx handler through my code behind. I am using asp.net with C#. I know that it can be called by Response.write("") writing the whole script part. Ans also Register Script of page method.
But Is there any way by which I can send request to handler and get back response from the handler using c#.

Thanks.

शेखर
  • 17,412
  • 13
  • 61
  • 117
  • I have found the similar question whose link is below http://stackoverflow.com/questions/6865025/server-transfer-to-an-httphandler – शेखर Jul 18 '12 at 06:56
  • But there too it takes context as parameter and also how i will get the response I don't know. Please help – शेखर Jul 18 '12 at 06:59

2 Answers2

22

You can call any http resource from code behind using HttpWebRequest (System.Net namespace)

Sample

HttpWebRequest request = (HttpWebRequest) WebRequest.Create("<YourDomain>/Services/name.ashx?CustMobile=ValueOfA&CustName=ValueOfB&CustEmail=ValueOfC");
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

You need an absolute path but you can get your domain or server url from the HttpContext if you dont want to hardcode the domain

More Information

Community
  • 1
  • 1
dknaack
  • 60,192
  • 27
  • 155
  • 202
  • I have a small question do I have to put the absolute path or relative path will work. Since it (handler) is a part of my application. – शेखर Jul 18 '12 at 07:02
  • You need an absolute path. You can get your domain or server url from the HttpContext if you dont want to hardcode the domain. – dknaack Jul 18 '12 at 07:03
  • (HttpWebResponse)request.GetRequestStream(); not able to cast it in response. – शेखर Jul 18 '12 at 07:17
  • Use (HttpWebResponse)request.GetResponse(); to get the respose. – MJ Fathinia Jul 18 '12 at 07:29
  • I am getting an error The remote server returned an error: (400) Bad Request. My http handler is also implementing IRequiresSessionState. Since I have to do some session stuff over there. I this can be a problem. – शेखर Jul 18 '12 at 09:03
  • Yes, if you have a session you have to maintain this. Depends on your session handling. – dknaack Jul 18 '12 at 09:10
  • 1
    thanks @dknaack I did not solved my problem due to some complex use of session but I was able to access that handler form your suggested way. The only problem was session. – शेखर Jul 18 '12 at 10:21
  • How can I send JSON data using this technique? – Aishwarya Shiva Apr 18 '14 at 15:33
6

I found another way of doing this. If you want to access it from the same project it's is very easy.

Steps to use it in code behind

  1. Basically it create a class with a class name.
  2. You can create a object of that class.
  3. Then you can call ProcessRequest.
  4. It will execute that handler.

Suppose I have created a handler as follows

public class HandlerName : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
       //some code
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

So you can use it as follows

HandlerName obj=new HandlerName();
obj.ProcessRequest(HttpContext);

Note that you can get the current context and you need to pass it in the process request. More about HttpContext [1 2]

Edit 1

You can also overload the ProcessRequest method in case if you need to do this.

public class HandlerName : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
      // some code
    }
    public void ProcessRequest(HttpContext context, string someString)
    {
       // do your coding here
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

If you don't want to override the method the you can pass the values as follow
You can add the value in the HttpContext.Current.Items

HttpContext.Current.Items["ModuleInfo"] = "Custom Module Info"

and get it as follow in the ProcessRequest Method

public class HandlerName : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
      string contextData = (string)(HttpContext.Current.Items["ModuleInfo"]);
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117