Home > Back-end >  Controller not working (except WeatherForecast) in .NET Core with React
Controller not working (except WeatherForecast) in .NET Core with React

Time:06-17

I created a fresh .NET Core with React.js project, which has this by default in Program.cs:

app.MapControllerRoute(
    name: "default",
    pattern: "{controller}/{action=Index}/{id?}");

app.MapFallbackToFile("index.html"); ;

The provided WeatherForecastController works, i.e. this works:

https://localhost:44449/weatherforecast

But if I copy WeatherForecastController.cs and name it MyNewController.cs like this, it won't work.

[ApiController]
[Route("[controller]")]
public class MyNewController : ControllerBase
{
   ...

https://localhost:44449/mynew returns the content of index.html.

What am I missing?

CodePudding user response:

In setupProxy.js you have to add /mynew in the context:

const context =  [
    "/weatherforecast",
    "/mynew",
];

module.exports = function(app) {
  const appProxy = createProxyMiddleware(context, {
    target: target,
    secure: false,
    headers: {
      Connection: 'Keep-Alive'
    }
  });

  app.use(appProxy);
};

Otherwise, the framework will interpret the url /mynew as a route to a screen and will handle it as such.

  • Related