---
title: "What are some approaches for error handling when consuming .NET Core APIs in ASP.NET MVC?"  
description: "What are some approaches for error handling when consuming .NET Core APIs in ASP.NET MVC?"  
author: "Rocky Dada"  
published: 2023-08-30  
updated: 2023-08-30  
canonical: https://www.mindstick.com/forum/159784/what-are-some-approaches-for-error-handling-when-consuming-dot-net-core-apis-in-asp-dot-net-mvc  
category: "web api"  
tags: ["c#", ".net", ".net core api"]  
reading_time: 3 minutes  

---

# What are some approaches for error handling when consuming .NET Core APIs in ASP.NET MVC?

Explain different [techniques](https://www.mindstick.com/articles/13015/5-practical-tips-and-techniques-to-write-an-essay) for [handling](https://www.mindstick.com/forum/34585/file-handling) errors when [consuming .NET](https://www.mindstick.com/forum/159785/how-do-you-implement-authentication-and-authorization-when-consuming-dot-net-core-apis-in-asp-dot-net-mvc) [Core APIs](https://www.mindstick.com/forum/160208/how-entity-framework-core-simplify-database-access-in-dot-net-core-apis) in an ASP.NET [MVC application](https://www.mindstick.com/forum/12932/how-to-consume-webservices-from-asp-dot-net-mvc-application). Discuss handling HTTP [status codes](https://www.mindstick.com/forum/160304/importance-of-http-status-codes-in-responses-to-httppost-methods-requests-in-dot-net-core-api), [exceptions](https://www.mindstick.com/interview/22871/define-predifined-generic-exceptions), and displaying appropriate [error messages](https://www.mindstick.com/forum/160245/how-to-customize-error-messages-with-a-data-annotation-in-asp-dot-net-core) to users.

## Replies

### Reply by Aryan Kumar

There are a few approaches for error handling when consuming .NET Core APIs in [ASP.NET MVC](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc). Here are a few of the most common methods:

- **Using the** `ExceptionHandlerAttribute`**:** The `ExceptionHandlerAttribute` is an attribute that can be applied to controller actions or methods to handle errors that occur during the execution of the action or method. The `ExceptionHandlerAttribute` takes a single parameter, which is the type of exception that the attribute should handle.
- **Using the** `try-catch` **block:** The `try-catch` block is a statement that allows you to handle errors that occur during the execution of a block of code. The `try-catch` block takes two statements, the `try` statement and the `catch` statement. The `try` statement contains the code that you want to execute, and the `catch` statement contains the code that you want to execute if an error occurs.
- **Using the** `Response.StatusCode` **property:** The `Response.StatusCode` property is a property that allows you to set the status code of the HTTP response. The status code of the HTTP response can be used to indicate the success or failure of the request.

Here is an example of how to use the `ExceptionHandlerAttribute` to handle errors when consuming a .NET Core API in ASP.NET MVC:

C#

```plaintext
[ExceptionHandler(typeof(Exception))]
public class HomeController
{
    public async Task<IActionResult> Index()
    {
        // This action will handle all exceptions.
        return View();
    }
}
```

Here is an example of how to use the `try-catch` block to handle errors when consuming a .NET Core API in ASP.NET MVC:

C#

```plaintext
public async Task<IActionResult> Index()
{
    try
    {
        // This code will attempt to call the .NET Core API.
        var response = await HttpClient.GetAsync("https://api.example.com/products");

        if (response.StatusCode == HttpStatusCode.OK)
        {
            // The request was successful.
            return View();
        }
        else
        {
            // The request failed.
            return RedirectToErrorPage();
        }
    }
    catch (Exception ex)
    {
        // An error occurred.
        return RedirectToErrorPage();
    }
}
```

Here is an example of how to use the `Response.StatusCode` property to handle errors when consuming a .NET Core API in ASP.NET MVC:

C#

```plaintext
public async Task<IActionResult> Index()
{
    try
    {
        // This code will attempt to call the .NET Core API.
        var response = await HttpClient.GetAsync("https://api.example.com/products");

        if (response.StatusCode == HttpStatusCode.OK)
        {
            // The request was successful.
            return View(response.Content.ReadAsStringAsync());
        }
        else
        {
            // The request failed.
            Response.StatusCode = response.StatusCode;
            return View();
        }
    }
    catch (Exception ex)
    {
        // An error occurred.
        Response.StatusCode = 500;
        return View();
    }
}
```

The best approach for error handling when consuming .NET Core APIs in ASP.NET MVC will depend on the specific needs of your application. You should consider the factors such as the complexity of the API, the severity of the errors that can occur, and the familiarity of your developers with the different techniques.


---

Original Source: https://www.mindstick.com/forum/159784/what-are-some-approaches-for-error-handling-when-consuming-dot-net-core-apis-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
