Net core application and calling other .Net Core Web API application. I am calling controller methods as below
public async Task<T> SendRequest<T, U>(U request, string methodName, HttpMethod reqtype, string token)
{
T res = default(T);
try
{
string url = methodName;
string contentJson = JsonConvert.SerializeObject(request);
var content = new StringContent(contentJson, Encoding.UTF8, "application/json");
var actualRequest = new HttpRequestMessage(reqtype, url);
actualRequest.Content = content;
_httpClient.DefaultRequestHeaders.Clear();
_httpClient.DefaultRequestHeaders.Add("Authorization", token);
var rawData = await _httpClient.SendAsync(actualRequest);
var processedData = await rawData.Content.ReadAsStringAsync().ConfigureAwait(false);
res = JsonConvert.DeserializeObject<T>(processedData);
}
catch (Exception ex)
{
_reqHandler.RaiseBusinessException(MethodBase.GetCurrentMethod()?.DeclaringType?.DeclaringType?.Name
, MethodBase.GetCurrentMethod().Name,
"InputType: " + typeof(U) + " || OutputType: " + typeof(T) + " || Error Message: " + ex.Message);
}
return res;
}
Below are the samples returning from API controller
return StatusCode(404, result.MapFileFileUploadSummary);
return StatusCode((int)System.Net.HttpStatusCode.Unauthorized);
In the above code when I return some status code I want to bind it to genric type T. For example I am calling one method
_Client.SendRequest<ObjectResult, requestModel>()
I am sending ObjectResult of type as return type. I am getting below response in processedData for example,
"{\"type\":\"https://tools.ietf.org/html/rfc7235#section-3.1\",\"title\":\"Unauthorized\",\"status\":401,\"traceId\":\"00-e517f28f9c0b0441a8634c1c703a1cae-f64589d11056ad45-00\"}"
But unable to bind it to
res = JsonConvert.DeserializeObject<T>(processedData);
This returns nothing I am adding screenshot here
here T is of type ObjectResult. I am looking for generic response type here so that I can always bind it to type T. Can some one help me with this implementation. Any help would be appreciated. Thank you