locked
Need to check the result from API before returning the value. RRS feed

  • Question

  • 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; }
    		}
    


     

    Wednesday, January 8, 2020 12:18 PM

All replies

  • User475983607 posted

    Use a variable to rack the results of the API calls.  Pass the variable to the View and display any HTML you like.

    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;
    	
    	bool result = RegistrationResponse.Success;
    	
    	//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;
    	
    	ViewBag.Result = result & RegistrationResponse.Success;
    	
    	//if (!RegistrationResponse.Success)
    	//{
    	//    ViewData["ErrorMessage"] = RegistrationResponse.SystemMessage;
    	//}
    	//RegisteredCarriersAndDocuments.RegisteredDocs = RegistrationResponse.RegisteredDocuments;
    	return View(RegisteredCarriersAndDocuments);
    }

    @if((bool)ViewBag.Result)
    {
    	//Success
    }
    else 
    {
    	//Failed
    }

    Wednesday, January 8, 2020 12:50 PM