C# 如何调用 ASHX 处理程序并取回结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14519757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to call ASHX handler and getting the result back
提问by patel.milanb
I have created a Handler which return integer value after doing some database work. i would like to know how can i get that value and assign that value to Label by calling that handler.
我创建了一个处理程序,它在执行一些数据库工作后返回整数值。我想知道如何通过调用该处理程序来获取该值并将该值分配给 Label。
I have googled it and most of the example uses Jquery.AJAX calls to retrieve the value. I am sure i can also get the value by using that. BUT for some limitation in my company i am restricted to use code behind.
我用谷歌搜索过,大部分示例使用 Jquery.AJAX 调用来检索值。我相信我也可以通过使用它来获得价值。但是由于我公司的一些限制,我被限制使用后面的代码。
Any example will help.
任何例子都会有所帮助。
Handler: http://somesite.com/Stores/GetOrderCount.ashx?sCode=VIC
which returns: 3
need to assign this to a label control
需要将此分配给标签控件
i have tried this much so far.
到目前为止,我已经尝试了很多。
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
采纳答案by nunespascal
WebClient client = new WebClient ();
Label1.Text = client.DownloadString ("http://somesite.com/Stores/GetOrderCount.ashx?sCode=VIC");
You could also directly call your handler using ajax and update the label.
您还可以使用 ajax 直接调用您的处理程序并更新标签。
Here is a jQuery example:
这是一个 jQuery 示例:
$.get('Stores/GetOrderCount.ashx?sCode=VIC', function(data) {
$('.result').html(data);
});
回答by Quannt
Try this
尝试这个
System.IO.Stream stream = response.GetResponseStream();
System.IO.StreamReader reader = new System.IO.StreamReader(stream);
string contents = reader.ReadToEnd();