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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
SriVaddadiConnect With a Mentor Commented:
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for 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"  ???

Thanks.
0
 
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
 
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
All Courses

From novice to tech pro — start learning today.