简体   繁体   English

如何将通用处理程序绑定到ASP.NET中的Label.Text

[英]How to bind generic handler to Label.Text in ASP.NET

I am trying to bind output of generic handler to Label. 我正在尝试将通用处理程序的输出绑定到Label。 Make it simple. 简单点。 My handler only write the "Hello World" string. 我的处理程序仅写入“ Hello World”字符串。

public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
    }

And I want to bind this to some label in aspx page. 我想将此绑定到aspx页面中的某些标签。 I am trying this code, but it doesnt work. 我正在尝试此代码,但是它不起作用。

HttpWebRequest request = (HttpWebRequest)WebRequest.Create("../Handler1.ashx");
        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        using (response)
        {
            StreamReader reader = new StreamReader(response.GetResponseStream());
            reader.ReadToEnd();
            Label1.Text = reader.ReadLine();

        }

It raises error bad Uri. 它引发错误错误Uri。 Thanks for your ideas and comments. 感谢您的想法和意见。 :) :)

Similar to this - How to call HttpHandler from .cs file asp.net 与此类似- 如何从.cs文件asp.net中调用HttpHandler

You need to provide an absolute URI. 您需要提供一个绝对URI。 Also note that the ReadToEnd() call will do precisely that; 还要注意, ReadToEnd()调用将精确地做到这一点; subsequently calling ReadLine will return an empty string. 随后调用ReadLine将返回一个空字符串。

    HttpWebRequest  request  = (HttpWebRequest)WebRequest.Create("http://localhost:11034/handlers/handler1.ashx");
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();

    using (response)
    {
        StreamReader reader = new StreamReader(response.GetResponseStream());
        Label1.Text = reader.ReadToEnd();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM