Calling a generic handler from C#

Hi.

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 ?

Thanks.
jonatecAsked:
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.

xoperatorCommented:
Hey,

Here is a perfect solution for you:

http://www.ajaxprojects.com/ajax/tutorialdetails.php?itemid=675

0
xoperatorCommented:
Please tell me if it works, thank you.
0
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"  ???

Thanks.
0
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 ??
0
SriVaddadiCommented:
Request to your page should do the trick
0
jonatecAuthor Commented:
Need a code example please.
0
SriVaddadiCommented:
0
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.
0
SriVaddadiCommented:
Response.Redirect(<your url as mentioned above>) or Server.Transfer(<your url as mentioned above>)
0
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.
0
SriVaddadiCommented:
You have use HttpWebRequest class. For example please go thru the below link


http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse(VS.71).aspx
0

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);
      newStream.Close();      

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

      txtResult.Text = reader.ReadToEnd();

      reader.Close();
      response.Close();
    }
0
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
ASP.NET

From novice to tech pro — start learning today.