Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Dev server cannot reach the ASP.NET Core API in a Visual Studio SPA project

Errorhttp proxy error at /weatherforecast

Short answer

The dev server forwards /weatherforecast to the ASP.NET Core backend. The error means nothing answered at the target address. Check three things: the backend started first, the port in the proxy target matches applicationUrl in launchSettings.json, and with Docker the target uses the container's HTTPS port.

  • Applies to Visual Studio 2022 17.8+, Vite 7
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find the backend address and see whether anything listens there. Take the https applicationUrl from launchSettings.json.

    grep -n "applicationUrl" Properties/launchSettings.json
    curl -k -I https://localhost:7049/weatherforecast
  2. 2Match what you see, then jump to its fix

    1. 1ECONNREFUSED on the first request only; refreshing the browser later worksThe front end started before the backendFix
    2. 2ECONNREFUSED to a port the backend never uses, for example 5001 while launchSettings.json says 7049The proxy target port does not match the backend portFix
    3. 3The API works inside Visual Studio's Containers window, but the proxy target is still the old localhost portThe backend runs in Docker on a different HTTPS portFix

What does the error look like?

  • http proxy error at /weatherforecast
  • [HPM] Error occurred while trying to proxy request /weatherforecast from localhost:4200 to https://localhost:5001 (ECONNREFUSED)
  • Weather forecast data never loads in the browser

Cause 1 of 3

The front end started before the backend

1Check

Watch the two console windows Visual Studio opens. If the Angular or Vite window shows the proxy error before the API window says it is listening, the order is wrong.

dotnet run --project AngularWithASP.Server

2Fix

Wait until the backend window is running, then refresh the browser. Make the ASP.NET Core project first in the startup list (Solution Properties, Configure Startup Projects).

# Visual Studio: Solution Properties > Startup Project > Multiple projects > Action: Start, backend first

3Verify

Start with F5 again; the weather data should load without a manual refresh.

curl -k https://localhost:7049/weatherforecast

Confirmed by learn.microsoft.com

Did this fix it?

Cause 2 of 3

The proxy target port does not match the backend port

1Check

Compare the port in the proxy file (proxy.conf.js, or the server.proxy section of vite.config) with the https applicationUrl in the backend's launchSettings.json.

grep -n "applicationUrl" Properties/launchSettings.json
grep -n -E "target|proxy" vite.config.* proxy.conf.js 2>/dev/null

2Fix

Set the proxy target to the https applicationUrl from launchSettings.json.

// proxy.conf.js (Angular template)
target: 'https://localhost:7049',

// vite.config.js (Vite based template)
server: {
  proxy: {
    '/weatherforecast': {
      target: 'https://localhost:7049',
      changeOrigin: true,
    },
  },
},

3Verify

Restart the dev server, because proxy settings are read at start-up.

curl -I http://localhost:4200/weatherforecast

Confirmed by learn.microsoft.com

Did this fix it?

Cause 3 of 3

The backend runs in Docker on a different HTTPS port

1Check

Open the Containers window in Visual Studio and read the HTTPS port on the Environment or Ports tab.

docker ps --format "table {{.Names}}\t{{.Ports}}"

2Fix

Update the target to that HTTPS port. If the port is not shown, add sslPort to the Container (Dockerfile) profile in launchSettings.json, then restart.

const target = env.ASPNETCORE_HTTPS_PORT
  ? `https://localhost:${env.ASPNETCORE_HTTPS_PORT}`
  : 'https://localhost:62958';

3Verify

Restart the app and reload the page.

curl -k -I https://localhost:62958/weatherforecast

Confirmed by learn.microsoft.com

Did this fix it?

How do I stop it coming back?

  • Keep the backend first in the multiple-startup-projects list.
  • After changing launchSettings.json, update the proxy target in the same commit.
  • Read the proxy target from an environment variable instead of a hard-coded port.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Related fixes

Web apps

The Vite dev proxy does not forward my /api requests

The browser calls the backend directly instead of going through the Vite dev server

Vite proxies only requests whose path starts with a key in server.proxy, and it forwards the full path unless you rewrite it.

5 causes, each with a fix

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.