如何调用ASHX处理程序并返回结果

我创建了一个Handler,在执行一些数据库工作后返回整数值。 我想知道如何通过调用该处理程序获取该值并将该值赋给Label。

我用google搜索它,大多数示例使用Jquery.AJAX调用来检索值。 我相信我也可以通过使用它获得价值。 但由于我公司的某些限制,我被限制使用代码。

任何例子都会有帮助。

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

需要将其分配给标签控件

到目前为止,我已经尝试了这么多。

 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 

使用WebClient.DownloadString

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

您也可以使用ajax直接调用您的处理程序并更新标签。

这是一个jQuery示例:

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

试试这个

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