Share via


Sending a response to "await response.Content.ReadAsStringAsync()"

Question

Thursday, November 7, 2019 11:57 AM

I am using an Azure function to trigger an activity on my Web Pages site (not on Azure)

static async Task myPost(TraceWriter log)
{
    HttpClient client = new HttpClient();

    var values = new Dictionary<string, string>
    {
    { "source", "BT" },
    { "action", "1" }
    };

    var content = new FormUrlEncodedContent(values);
    var response = await client.PostAsync("http://www.mysite.com/SubscriptionCheck.cshtml", content);
    var responseStr = await response.Content.ReadAsStringAsync();
    log.Info($"C# Post Response: {responseStr}");

}

On my site I have a listener:

string source = Request["source"];
string action = Request["action"];

... do stuff

When I am done doing stuff I would like to send a reply so when I am logging the Azure function I can see the response.

So far I have not been able to find the right magic words to unlock this secret from google.

How can I do this in Web Pages?

All replies (8)

Thursday, November 7, 2019 3:53 PM âś…Answered

wavemaster

responseStr is null because there is nothing in my listener that sends a response.

My listener has no functionality to send a response.

What code do I need in my listener to send a response (in a string)?

I have no idea what the "listener" is.  The only items listening are the the web page and the function.  Are you asking how to write code in a web page to return a message?

    public partial class SubscriptionCheck : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Clear();
            Response.ContentType = "text/html; charset=UTF-8";
            Response.Write("Hello World");
            Response.Flush();
            Response.End();

        }
    }

Thursday, November 7, 2019 12:04 PM

When I am done doing stuff I would like to send a reply so when I am logging the Azure function I can see the response.

So far I have not been able to find the right magic words to unlock this secret from google.

How can I do this in Web Pages?

What do you want to return?  XML? JSON? a string?  What do you see now?  Have you verified the Web Page is functioning as expected?


Thursday, November 7, 2019 12:07 PM

I want to send a string back, something along the lines of "X no of records processed"

Currently responseStr = await response.Content.ReadAsStringAsync();  returns nothing


Thursday, November 7, 2019 12:25 PM

I want to send a string back, something along the lines of "X no of records processed"

Currently responseStr = await response.Content.ReadAsStringAsync();  returns nothing

I would start systematically troubleshooting.  First, copy the code shown above into a console application and test.  That way you can isolate the web page and run the web page in the debugger.  If there is a problem you should be able to find the bug with the Visual Studio debugger.  Once see a response form the page the move on to return a message.  

The basica idea is clear the response object and return a custom response.  This is similar to returning a file.  You can Google this it's pretty simple.

At this point, you have working code.  Move the code to the function and test again.  If you are not getting the expected results at least you know the code works.  Now you can investigate why the log is not working as expected.


Thursday, November 7, 2019 2:39 PM

The first code block up above works.

source and action are passed to the listener.

"do stuff" (from the second code block) is successful

responseStr = null

The question is what code do I need to return from my listener soI can retrieve "X" from responseStr? 

var X = something.Count;

 ??

Thursday, November 7, 2019 3:31 PM

You're missing the point.  The community cannot speculate why responseStr is null as we cannot see the web page code.   This is why I recommended troubleshooting to eliminate possible issues .  First isolate the web page logic.  Make sure you can hit the page and get a response using a console application.  This step should take a few minutes as you already have the code.  Once you can get a response from the web page, work on returning a formatted message.  Once you prove the client and web page function as expected move the code the function.

At this point the error can be anywhere in your code.  You need to figure out where to focus your attention. 


Thursday, November 7, 2019 3:34 PM

responseStr is null because there is nothing in my listener that sends a response.

My listener has no functionality to send a response.

What code do I need in my listener to send a response (in a string)?


Thursday, November 7, 2019 7:28 PM

That was it!

My apologies for not clarifying that the code was working correctly.