One DotNET API Controller Returning HTML Only to React

Viewed 25

I have two identical controllers, "WeatherForecastController" (from the stock DotNET API / React project, but gutted), and "ExampleReactController". Both are identical:

using Microsoft.AspNetCore.Mvc;

namespace DotNetReact_BackEnd.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static List<string> Data = new List<string>() { "Testing", "123" };

        [HttpGet]
        public IEnumerable<string> Get()
        {
            return Data;
        }
    }
}



using Microsoft.AspNetCore.Mvc;

namespace DotNetReact_BackEnd.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class ExampleReactController : ControllerBase
    {
        private static List<string> Data = new List<string>() { "Testing", "123" };

        [HttpGet]
        public IEnumerable<string> Get()
        {
            return Data;
        }
    }
}

My proxy is configured:

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

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

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

    app.use(appProxy);
};

Both work with Swagger, and both work with Postman, but for some reason only the WeatherForecastController works with react:

async populateStrings() {
        const response = await fetch('weatherforecast');
        console.log(response);
        const data = await response.json();
        console.log(data);
        this.setState({ forecasts: data, loading: false });
    }

This returns Array(2).

async populateStrings() {
    const response = await fetch('examplereact');
    console.log(response);
    const data = await response.json();
    console.log(data);
    this.setState({ forecasts: data, loading: false });
}

But this returns 'Uncaught (in promise) SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON'

1 Answers

Solved: Deleted the node modules folder and did an npm install restore and somehow that fixed it. Something must've been corrupted or cached and not overwritten.

Related