简体   繁体   中英

React setupProxy not proxying request

I'm trying to setup a React web application using an asp.net core web API in two separate projects. I followed Microsoft's guide for setting up the templates using two separate visual studio projects - Create an ASP.NET Core app with React - https://learn.microsoft.com/en-us/visualstudio/javascript/tutorial-asp-net-core-with-react?view=vs-2022

Out of the box, everything works and I am able to proxy my api request to the sample /weatherforecast controller's get method. Next, I created my own controller and now I don't seem to be able to proxy my request to this controller.

Making a request to https://localhost:3000/api/employee just returns 404. I also see a message in my console about no matching route.

I've tried various patterns for the context all with no luck. I would think that '/api' is all I need.

My setupProxy.js file looks like this.

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

module.exports = function (app) {
    app.use(
        ['/api/weatherforecast', '/api/employee'],
        createProxyMiddleware({
            target: 'https://localhost:7157',
            secure: false,
        })
    ); };

My launch settings.json in my web api project

{
  "profiles": {
    "WebApi": {
      "commandName": "Project",
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      },
      "dotnetRunMessages": true,
      "applicationUrl": "https://localhost:7157;http://localhost:5157"
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  },
  "$schema": "https://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:34955",
      "sslPort": 44352
    }
  }
}

Partial of My Employee Controller

namespace WebApi.Controllers {
    [ApiController]
    [Route("api/[controller]")]
    public class EmployeeController : Controller
    {
        private readonly ILogger<EmployeeController> logger;
        private readonly Context context;
        
        

        public EmployeeController(ILogger<EmployeeController> logger, TenthContext context)
        {
            this.logger = logger;
            this.context = context;
        }



        // GET: EmployeeController
        [HttpGet(Name="GetEmployee")]
        public IEnumerable<Employee> Get()
        {
            var employees = context.Employees.ToList();
            return employees;
        }

Not sure what the deal was but all worked as expected after I rebooted.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM