An error occurred while sending the request. getting this error intermediate in mvc

jagan.ss 6 Reputation points
2021-02-22T07:40:37.767+00:00

An error occurred while sending the request.===22/02/2021 09:24:45
------------------------------Stack Trace--------------------------------------------
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Areas.PPP.Controllers.TransactionController.<EditTransaction>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.<BeginInvokeAction>b__1c()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult)===22/02/2021 09:24:45

I am getting above error message while call the API from my controller. Controller code

controller code


[HttpGet]
public async Task<ActionResult> EditTransaction()
{
Session[ClientConstant.THEME_CLAPPER_TOGGLE] = ClientConstant.THEME_CLAPPER;
TransactionSetUpViewModel ViewModel = new TransactionSetUpViewModel();
long transactionHeader = (long)TempData[CARRIER_ID_FOR_TEMP];
TempData.Keep(CARRIER_ID_FOR_TEMP);
var uri = string.Format("{0}/{1}/{2}/{3}", ClientConstant.WEBAPI_URI_TRANSACTION, API_TRANSACTIONGETEDITMODEL,
transactionHeader, (int)ActionModes.EDIT);
HttpClient client = GetHTTPClient(uri);
HttpResponseMessage responseMessage = await client.GetAsync(uri);
if (responseMessage.IsSuccessStatusCode)
{
var responseData = responseMessage.Content.ReadAsStringAsync().Result;
try
{
ViewModel = JsonConvert.DeserializeObject<TransactionSetUpViewModel>(responseData);
if (ViewModel.PreDefinedList == null)
{
ViewModel.PreDefinedList = new List<SelectListItem>();
}
ViewModel.ActionMode = (int)ActionModes.EDIT;
ViewModel.IsOnlineMode = ClientConstant.ISONLINE_MODE;
return View("TransactionAction", ViewModel);
}
catch (System.Exception ex)
{
throw ex;
}
}
return View(ClientConstant.VIEW_ERROR);

}

HTTP client

public HttpClient GetHTTPClient(string uri)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(uri);
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(ClientConstant.HTTP_TYPE));
return client;

}

In global.asax in application start event i added the below code also

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,287 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,304 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points
    2021-02-22T13:32:22.61+00:00

    Well first off, a viewmodel doesn't travel. A DTO travels between processes, layers and tiers. The DTO is received by the the WebApi client from the WebAPI service. And the viewmodel object that belongs on the client side only is populated from the DTO.

    https://learn.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-5

    https://www.codeproject.com/articles/1050468/data-transfer-object-design-pattern-in-csharp

    You can use a global exception handler (GEH) in the WebApi to log the exception, the stack trace and any inner.excpetion.message if not null with the inner.exception.message giving further details about the exception possibly.

    Log it all to a log file by using Log4Net or some other logging framework.

    https://stackify.com/csharp-catch-all-exceptions/

    You don't need any try/catch in any code in the WebAPI if using GEH in the WebAPI. You don't need a try/catch in any code on the MVC when using a GEH on the MVC client side.