ASP.NET Core MVC Interview Question #186
How would you handle an external API failure from an MVC action?
Senior Practical MVC Scenarios & Production Architecture Senior Advanced
Quick Interview Answer
Keep HTTP integration logic in a dedicated service rather than the controller. Configure HttpClient through IHttpClientFactory and apply suitable timeouts and resilience policies according to the operation.
Detailed Explanation
Keep HTTP integration logic in a dedicated service rather than the controller. Configure HttpClient through IHttpClientFactory and apply suitable timeouts and resilience policies according to the operation.
The controller should receive a meaningful application result and decide how to present it. Do not expose raw upstream exception details to users, and do not retry non-idempotent operations blindly.
Code Example
public async Task<IActionResult> ShippingQuote(
int orderId,
CancellationToken token)
{
var result = await _shipping.GetQuoteAsync(orderId, token);
if (!result.Succeeded)
return View("ServiceUnavailable");
return View(result.Value);
}