webapi Microsoft.AspNetCore.Mvc.ObjectResult can be read by Content.ReadAsAsync<string> but using HttpReponseMessage.Content throws exception

steve fred 1 Reputation point
2023-05-18T03:46:58.8833333+00:00

I am trying to return an encrypted json string from an webapi controller get method.

If the method return type is and actionmethod an Microsoft.AspNetCore.Mvc.ObjectResult is return as shown:

public async Task<ActionResult> GetFileDownloadCompleteInfo1Async(int id)
{.....
string jString = Newtonsoft.Json.JsonConvert.SerializeObject(results.ResultContent);
var jsonStringEnc = jString.AESEncryption(SRFResources.SKeyJson, SRFResources.SVecJson);
 var ret= await Task.FromResult<ActionResult>(StatusCode(200, jsonStringEnc));
 return ret;

Now the HttpResponseMessage which is return from the call to the by the HttpClient to the GetFileDownloadCompleteInfo1, the HttpResponseMessage Content ReadAsAsync can be called as shown bellow returns the encrypted Json string.

the  var r = responseMessage.Content.ReadAsAsync<string>().Result; 

I am trying to find a way to retrieve the encrypted Json string when the controller action method returns a HttpResponseMessage as shown

public async Task<HttpResponseMessage > GetFileDownloadCompleteInfoAsync(int id)
{.....
string jString = Newtonsoft.Json.JsonConvert.SerializeObject(results.ResultContent);
var jsonStringEnc = jString.AESEncryption(SRFResources.SKeyJson, SRFResources.SVecJson);
var res1 = new HttpResponseMessage(HttpStatusCode.OK);
res1.Content = new StringContent(jsonStringEnc1, Encoding.ASCII, "application/json");
return res;

When the .ReadAsAsync is here an exception is thrown.

I tried many various other ways of setting the Content in GetFileDownloadCompleteInfoAsync using the encrypted json string, and called the various httpresponsemessage Content message to retrieve the encrypted json string but every way has failed.

So I am looking code to set the Content in controller method which contains the encrypted json string, and then how retrieve the encrypted json string on the httpresponsemessage which is returned from the httpclient.

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,150 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,249 questions
{count} votes