Calling a generic handler from C#


I can invoke a generic handler from JavaScript ok. I can also set an image url to an event handler location to process an image ok. However what if I want to return JSON data and return it to my behind code (C#)), how do I invoke a handler to do that ?

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.


Here is a perfect solution for you:

Please tell me if it works, thank you.
jonatecAuthor Commented:
Sorry, can't see the wood for the trees...

If I have a simple C# web project, and this has a simple generic handler called Handler.ashx how do I invoke this from C# server-side code to return plain text "Hello World"  ???

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

jonatecAuthor Commented:
SriVaddad ~

Yes, registered:

        <add verb="*" path="*.asmx" type="Handler1"/>
        <add verb="GET,POST" path="*.asmx" type="Handler1"/>

Although not sure if both entries are needed ?

Anyway, now that's done, how do I invoke it from my behind C# code ??
Request to your page should do the trick
jonatecAuthor Commented:
Need a code example please.
jonatecAuthor Commented:
That doesn't look like C# to me. I take it you don't know how to write the call in behind-code C#? No problem, if you can't provide an adequate code example, please back-off and let some else have a crack. Cheers.
Response.Redirect(<your url as mentioned above>) or Server.Transfer(<your url as mentioned above>)
jonatecAuthor Commented:
SriVaddadi. Your last code line; that just sends the response back to the browser.

As per my original question; I need to capture the response in C# and do something with it, for example, (and I use this purely as an example), form up some JSON text and return that using a standard ASP.NET callback. I can handle the later, I may even use jQuery to handle the AJAX, but what I don't know how to do is the former, ie. capture the response from the generic handler and process it internally in my behind-code.
You have use HttpWebRequest class. For example please go thru the below link

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jonatecAuthor Commented:
Yes that's much better, although I had to work out the C# for myself. For reference this code chunk works:

    protected void btnGo_Click(object sender, EventArgs e)
      // Set up URI of generic handler, will only work when set up in IIS (localhost), not in debug only mode.
      String sURI = String.Empty;
      sURI += "http://localhost/WebApplication2/Handler1.ashx";
      sURI += "?params=" + txtDay.Text;

      // Must set up querystring parameter length.
      ASCIIEncoding encoding = new ASCIIEncoding();
      byte[] data = encoding.GetBytes(txtDay.Text);

      // Set up Request.
      HttpWebRequest request = WebRequest.Create(sURI) as HttpWebRequest;
      request.ContentType = "text/plain";
      request.Method = "POST";
      request.Credentials = CredentialCache.DefaultCredentials;
      request.ContentLength = data.Length;

      // Send Request.
      Stream newStream = request.GetRequestStream();
      newStream.Write(data, 0, data.Length);

      // get Response.
      HttpWebResponse response = (HttpWebResponse)request.GetResponse();
      StreamReader reader = new StreamReader(response.GetResponseStream());

      txtResult.Text = reader.ReadToEnd();

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.