User1182587605 posted
I am reading values from an API and am setting them to show them outside. I want to check if the result is success or not before displaying or returning it. Please check my code and model and suggest me what I can do to check the result and return only if
it is success. My code is:
public IActionResult Index()
{
Token = GetToken();
AuthenticatedUser = Security.GetUserFromToken(Token);
string RegisteredCarriersJson = Common.Http.Get(AuthenticatedUser.AdministrationServicesHost, "Get/RegisteredCarrierIdentities?token=" + Token);
RegisteredCarriersAndDocuments.RegisteredCarriers = JsonConvert.DeserializeObject<ServiceResponse<List<RegisteredCarrier>>>(RegisteredCarriersJson).Result;
//if (!RegistrationResponse.Success)
//{
// ViewData["ErrorMessage"] = RegistrationResponse.SystemMessage;
//}
//RegisteredCarriersAndDocuments.RegisteredCarriers = RegistrationResponse.RegisteredCarriers;
string RegisteredDocsJson = Common.Http.Get(AuthenticatedUser.AdministrationServicesHost, "Get/RegisteredDocumentIdentities?token=" + Token);
RegisteredCarriersAndDocuments.RegisteredDocs = JsonConvert.DeserializeObject<ServiceResponse<List<RegisteredDocument>>>(RegisteredDocsJson).Result;
//if (!RegistrationResponse.Success)
//{
// ViewData["ErrorMessage"] = RegistrationResponse.SystemMessage;
//}
//RegisteredCarriersAndDocuments.RegisteredDocs = RegistrationResponse.RegisteredDocuments;
return View(RegisteredCarriersAndDocuments);
}
The commented section is an assumptive comparision. I need to check if both the Get methods succeed and then return them only on success. My Model is:
public class ServiceResponse<T> : ServiceResponse
{
public T Result { get; set; }
}
/// <summary>
/// The Web API-compliant class to represent a response to a service request.
/// </summary>
public class ServiceResponse
{
public bool Success { get; set; }
public int ErrorCode { get; set; }
public string DisplayMessage { get; set; }
public string SystemMessage { get; set; }
public string ExceptionDetail { get; set; }
}