Evaluate data from an ActionResult
How do I evaluate messages from an Api access?
http post Reply
So you can evaluate ActionResult text messages from an Asp.Net Core Api response.
Situation: The client sends a client request to the web server with httpPost. The web server responds in the Api method with an ActionResult response.
In the server
Asp.Net Core Web API
ActionResult response= Json(new { status = "OK", message = 415 }); return response; |
In the client
Upload data to Web Api
HttpResponseMessage httpResponseMessage = await client.PostAsync(sUrl_Api_Create, string_to_Upload_Content ); |
And evaluating the response from the server
string sResult = await httpResponseMessage.Content.ReadAsStringAsync() ; |
Result:
In the client
This results in the following result
Result = {"status":"OK","message":415} |
Now of course you have to evaluate this information.
Because it is obviously a Json answer.
//< result > string sResult = await httpResponseMessage.Content.ReadAsStringAsync() ; //</ result >
//*like: {"status":"OK","message":415} JsonObject jsonResult = JsonObject.Parse(sResult); string sMessage = jsonResult.GetNamedValue("message").ToString();
|
Code Example in the UWP Client:
//< read webApi > //*httpPut /api/projects string sUrl_Api_Create = app_settings._Api_Server + "/api/projects"; HttpResponseMessage httpResponseMessage = null; try { //< create Upload_Content > JsonObject jsonObject = new JsonObject(); jsonObject["title"] = JsonValue.CreateStringValue(sTitle); jsonObject["html"] = JsonValue.CreateStringValue(sHTML); jsonObject["url"] = JsonValue.CreateStringValue(sURL); jsonObject["ort"] = JsonValue.CreateStringValue(sOrt); jsonObject["bereiche"] = JsonValue.CreateStringValue(sBereiche); jsonObject["gehalt"] = JsonValue.CreateStringValue(sGehalt); jsonObject["art"] = JsonValue.CreateStringValue(sArt);
StringContent string_to_Upload_Content = new StringContent(jsonObject.Stringify()); //</ create Upload_Content >
//< upload > //httpPost=Create httpResponseMessage = await client.PostAsync(sUrl_Api_Create, string_to_Upload_Content ); //</ upload >
if (httpResponseMessage.StatusCode==System.Net.HttpStatusCode.OK) {
} else { fx_Log("Upload Error=" + httpResponseMessage.ReasonPhrase); } //< result > string sResult = await httpResponseMessage.Content.ReadAsStringAsync() ; //</ result >
//*like: {"status":"OK","message":415} JsonObject jsonResult = JsonObject.Parse(sResult); string sMessage = jsonResult.GetNamedValue("message").ToString();
|
Note that the status code of the httpResponseMessage is still 200.
You can therefore give any ActionResult answer a text as content.
Debugger values ββin the breakpoint of the HttpResults
? |
Name |
Value |
Type |
β’ |
httpResponseMessage |
{StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Server: Kestrel Transfer-Encoding: chunked Date: Tue, 03 Jul 2018 11:56:44 GMT X-Powered-By: ASP.NET X-SourceFiles: =?UTF-8?B?xxxxx==?= Content-Type: application/json; charset=utf-8 }} |
System.Net.Http.HttpResponseMessage |
βΆ Content |
{System.Net.Http.StreamContent} |
System.Net.Http.HttpContent {System.Net.Http.StreamContent} |
|
βΆ Headers |
{Server: Kestrel Transfer-Encoding: chunked Date: Tue, 03 Jul 2018 11:56:44 GMT X-Powered-By: ASP.NET X-SourceFiles: =?UTF-8?B?xxxxx==?= } |
System.Net.Http.Headers.HttpResponseHeaders |
|
IsSuccessStatusCode |
true |
bool |
|
ReasonPhrase |
"OK" |
string |
|
βΆ RequestMessage |
{Method: POST, RequestUri: 'http://localhost:51198/api/projects', Version: 2.0, Content: System.Net.Http.StringContent, Headers: { Authorization: Bearer xxx.xxx Content-Type: text/plain; charset=utf-8 Content-Length: 1020 }} |
System.Net.Http.HttpRequestMessage |
|
StatusCode |
OK |
System.Net.HttpStatusCode |
|
βΆ Version |
{1.1} |
System.Version |
|
βΆ Static members |
|||
βΆ Non-Public members |
|||
sResult |
"{\"status\":\"OK\",\"message\":415}" |
string |