'Problems while adding new controller .NET 6 in react template
I created a new .NET 6 with react template (Visual Studio 2022).
I added a new API controller called ValuesController:
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
// GET: api/<ValuesController>
[HttpGet]
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
}
Inside the defalut FetchData.js page I changed to:
async populateWeatherData() {
const response = await fetch('api/values');
console.log(await response.text());
}
When I click F12 in the browser the response I get is:
The development server has disconnected.
--> Unlike "/favicon.ico" or "favicon.ico", "/favicon.ico" will work correctly both with client-side routing and a non-root public URL.
Refresh the page if necessary.
FetchData.js:56Learn how to configure a non-root public URL by running
Project1npm run build.
<noscript>
You need to enable JavaScript to run this app.
</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
Also I tried to update inside setupProxy.js:
const context = [
"/weatherforecast",
"/values"
];
In other words I don't know why I don't get an answer from my controller.
Solution 1:[1]
- Inside
setupProxy.jsadd:
const context = [
"/weatherforecast",
"/values"
];
- Run
npm run build
Solved my problem
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user17450256 |
