要解决部署了dotnet core react应用程序,API路由未被识别的问题,可以按照以下步骤进行操作。
services.AddControllers();
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
这将确保API路由被正确配置和识别。
例如,如果在后端API中定义了一个名为"api/users"的路由,那么在React应用程序中的API调用应该使用相同的路径:
fetch('api/users')
.then(response => response.json())
.then(data => console.log(data));
在React应用程序的index.js文件中,添加如下代码:
const baseUrl = document.getElementsByTagName('base')[0].getAttribute('href');
然后,在API调用中使用baseUrl作为基本路径:
fetch(baseUrl + 'api/users')
.then(response => response.json())
.then(data => console.log(data));
这样做可以确保API调用使用正确的路径。
通过以上步骤,应该能够解决部署了dotnet core react应用程序,API路由未被识别的问题。如果问题仍然存在,请检查后端API的代码和配置,确保路由正确配置和匹配。