Question

I am trying to bind output of generic handler to Label. Make it simple. My handler only write the "Hello World" string.

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. 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. Thanks for your ideas and comments. :)

Was it helpful?

Solution

Similar to this - How to call HttpHandler from .cs file asp.net

You need to provide an absolute URI. Also note that the ReadToEnd() call will do precisely that; subsequently calling ReadLine will return an empty string.

    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();
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top