1

This seems like a simple problem to solve, but I can't figure it out at all. I have followed the React/ASP.NET quick start guide on learn.microsoft.com and I can get everything working just as they have detailed. Where I'm stuck is creating any new routes.

I've added a new controller from the Visual Studio template "API Controller with read/write actions": enter image description here

Then I try to test this route:

route I will be testing

I've tried testing it using the same method as the existing, working fetch call is made from the react app to the back end server:

    // The example used in the tutorial and it works OK:
    const response = await fetch('weatherforecast');
    const data = await response.json();
    this.setState({ forecasts: data, loading: false });
    // I have imitated this to send a fetch to the "api/test3" route, which doesn't work
    const response = await fetch('api/test3');
    const data = await response.json();
    this.setState({ forecasts: data, loading: false });

But I cant work out what is different for one to be failing and one to be working. I have updated the setupProxy.js file and added the new routes:

const { createProxyMiddleware } = require('http-proxy-middleware');

const context = [
    "/weatherforecast",
    "/test",
    "/api",
    "/api/test",
    "/api/test3",
    "/test2",
];

module.exports = function (app) {
    const appProxy = createProxyMiddleware(context, {
        target: 'https://localhost:7236',
        secure: false
    });

    app.use(appProxy);
};

I also tried adding these to my package.json:

  "private": true,
  "proxy": "https://localhost:7236",

I've also tried replacing all of the script in a new controller with the same script as in the working WeatherForecastController.cs from the tutorial. This gives the same error.

JimmyTheCode
  • 3,783
  • 7
  • 29
  • 71
  • OK, so it's now working. I had closed and reopened Visual Studio before and it didn't fix anything. I've just done it again now and it's fixed things. Utterly bewildering. – JimmyTheCode May 26 '22 at 11:05

1 Answers1

1

The problem resolved itself (as if by magic) when I restarted Visual Studio. I'd tried that once or twice already, so perhaps one of the changes I made helped resolve the issue:

  1. Add this to package.json
  "private": true,
  "proxy": "https://localhost:7236",
  1. Ensure setupProxy.js is updated with the necessary endpoints, as above.

Other solutions that I didn't try, but may also have solved my problem:

  1. Removing the service worker, as per this solution

I have to say I've lost a lot of time trying to debug Visual Studio & .NET issues only to find the issue resolved by restarting Visual Studio. It would be handy if there was a button/feature to reload VisualStudio; delete the cache; and do whatever else is necessary for my current code to compile. If anyone knows then please let me know.

JimmyTheCode
  • 3,783
  • 7
  • 29
  • 71
  • Hi JimmyTheCode, glad to know you've found the solution to resolve this issue! Please consider accepting it as the answer to change its status to Answered. See [can I answer my own question..](https://stackoverflow.com/help/self-answer), Just a reminder :) – Jiale Xue - MSFT May 30 '22 at 01:35
  • Thanks! I had just been waiting for it to allow me. Sorry if you took time to read it through unduly – JimmyTheCode May 30 '22 at 08:17