How to call the ASHX handler and return the result

I created a handler that returns an integer value after doing some database work. I would like to know how I can get this value and assign this Label value by calling this handler.

I have googled, and most examples use Jquery.AJAX calls to retrieve the value. I am sure that I can also get the value using this. BUT for some restrictions in my company I am limited to use the code behind.

Any example will help.

Handler: http://somesite.com/Stores/GetOrderCount.ashx?sCode=VIC which returns: 3 

you must assign this to the label control

I've tried a lot already.

 HttpWebRequest request = (HttpWebRequest) WebRequest.Create("http://somesite.com/Stores/GetOrderCount.ashx?sCode=VIC"); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Label1.Text = response.ToString() // this does not work 
+7
source share
2 answers

Use WebClient.DownloadString

 WebClient client = new WebClient (); Label1.Text = client.DownloadString ("http://somesite.com/Stores/GetOrderCount.ashx?sCode=VIC"); 

You can also directly call your handler using ajax and update the label.

Here is a jQuery example:

 $.get('Stores/GetOrderCount.ashx?sCode=VIC', function(data) { $('.result').html(data); }); 
+11
source

try it

 System.IO.Stream stream = response.GetResponseStream(); System.IO.StreamReader reader = new System.IO.StreamReader(stream); string contents = reader.ReadToEnd(); 
+4
source

All Articles