Readdy Write  
0,00 €
Your View Money
Views: Count
Self 20% 0
Your Content 60% 0

Users by Links 0
u1*(Content+Views) 10% 0
Follow-Follower 0
s2*(Income) 5% 0

Count
Followers 0
Login Register as User

Evaluate Return Data-Messages from an Api ActionResult

03.07.2018 (πŸ‘11542)

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