Web API  

How to Fix “Network Error” in Axios While Calling .NET API?

The “Network Error” in Axios is a common issue when a React, Angular, or other frontend application attempts to call an ASP.NET Core Web API but fails before receiving a valid HTTP response. Unlike a 4xx or 5xx status code, this error typically indicates that the request never successfully reached the .NET backend or was blocked by the browser. Understanding the root causes such as CORS misconfiguration, HTTPS issues, incorrect base URLs, SSL problems, or server downtime is essential for resolving this issue in production environments.

This article explains the most common causes of the Axios “Network Error” when calling a .NET API and provides practical solutions for each scenario.

1. Fix CORS Configuration in ASP.NET Core

The most common cause of Axios “Network Error” is CORS (Cross-Origin Resource Sharing) misconfiguration. If the frontend and backend run on different ports or domains, the browser blocks the request.

Configure CORS properly in Program.cs:

builder.Services.AddCors(options =>
{
    options.AddPolicy("AllowFrontend",
        policy => policy
            .WithOrigins("http://localhost:3000")
            .AllowAnyHeader()
            .AllowAnyMethod());
});

var app = builder.Build();

app.UseCors("AllowFrontend");

If credentials (cookies or authentication headers) are required:

policy.WithOrigins("http://localhost:3000")
      .AllowAnyHeader()
      .AllowAnyMethod()
      .AllowCredentials();

Improper CORS setup results in browser-blocked requests, which Axios reports as “Network Error.”

2. Verify API Base URL in Axios

Incorrect API URLs cause connection failures.

Incorrect:

axios.get("/api/products");

Correct:

axios.get("https://localhost:5001/api/products");

Always ensure:

  • Correct protocol (http vs https)

  • Correct port number

  • Backend server is running

For better maintainability, configure a base URL:

const api = axios.create({
  baseURL: "https://localhost:5001/api"
});

3. Fix HTTPS and SSL Certificate Issues

If your .NET API uses HTTPS with a development certificate, the browser may block requests due to untrusted SSL certificates.

Run:

dotnet dev-certs https --trust

Restart both backend and frontend after trusting the certificate.

Mixed content errors (HTTP frontend calling HTTPS backend or vice versa) can also trigger network errors.

4. Check Backend Server Status

If the ASP.NET Core Web API is not running or crashes during startup, Axios cannot establish a connection.

Verify:

  • The API is running

  • No runtime exceptions during startup

  • The correct launch profile is selected

  • Firewall is not blocking the port

Try accessing the API endpoint directly in the browser to confirm availability.

5. Handle Large Payload or Request Timeout Issues

If uploading large files or sending large JSON payloads, the server may reject the request.

Increase request size limit in ASP.NET Core:

builder.WebHost.ConfigureKestrel(options =>
{
    options.Limits.MaxRequestBodySize = 52428800; // 50 MB
});

Also configure Axios timeout:

axios.get("https://localhost:5001/api/data", {
  timeout: 10000
});

Timeout issues sometimes appear as network errors.

6. Verify Authorization Header Format

If using JWT authentication, ensure the Authorization header is properly formatted.

axios.get("https://localhost:5001/api/secure", {
  headers: {
    Authorization: `Bearer ${token}`
  }
});

Missing or malformed tokens may trigger preflight failures.

7. Inspect Browser Developer Tools

Open Developer Tools → Network tab and check:

  • Request URL

  • Response status

  • CORS error messages

  • Preflight (OPTIONS) request status

Console errors often reveal whether the issue is CORS, SSL, DNS, or server-related.

Common Causes and Fixes

IssueRoot CauseSolution
CORS errorMissing CORS policyConfigure UseCors properly
SSL issueUntrusted dev certificateTrust HTTPS certificate
Wrong URLIncorrect port or protocolFix baseURL
Server downAPI not runningStart backend
TimeoutLong-running requestIncrease timeout
Large payloadRequest size limit exceededIncrease server limit

Production Best Practices

  • Use environment variables for API base URLs

  • Configure strict CORS policies (avoid AllowAnyOrigin in production)

  • Use HTTPS in all environments

  • Implement proper logging in ASP.NET Core

  • Monitor failed requests using application monitoring tools

Diagnosing Axios network errors requires checking both frontend configuration and backend infrastructure.

Summary

The Axios “Network Error” when calling a .NET API usually indicates a client-side connectivity issue rather than a server response error. The most common causes include CORS misconfiguration, incorrect API base URLs, HTTPS certificate problems, server downtime, large payload limits, or malformed authorization headers. By properly configuring CORS in ASP.NET Core, verifying SSL certificates, ensuring correct Axios baseURL settings, and inspecting browser developer tools for detailed error messages, developers can quickly identify and resolve connectivity issues between React and .NET backend applications.