这是我的proxy.conf.js文件:
const PROXY_CONFIG = [
{
context: [
"/api/*",
],
target: "https://localhost:7139",
secure: false
}
]
module.exports = PROXY_CONFIG;
这是我为了测试目的创建的
TestController
控制器:[Authorize]
[ApiController]
[Route("api/[controller]")]
public class TestController : ControllerBase
{
private readonly ILogger<TestController> _logger;
public TestController(ILogger<TestController> logger)
{
_logger = logger;
}
[HttpGet("{id}")]
public string Get(int id)
{
return "Test response";
}
}
以下是我在Angular应用程序中尝试调用它的方法:
this.http.get<string>(`api/test/1`).subscribe((value) => {
alert(value);
},
(error) => {
alert(`Error: ${error.error}`);
});
这是我在
angular.json
的 serve
部分中添加 proxy.conf.js
的方式:"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"configurations": {
"production": {
"browserTarget": "angularproject1.Client:build:production"
},
"development": {
"browserTarget": "angularproject1.Client:build:development"
}
},
"defaultConfiguration": "development",
"options": {
"proxyConfig": "src/proxy.conf.js"
}
},
我已经检查了端口号,在proxy.conf.json
文件中,确实与ASP.NET Core项目中launchSettings.json
文件中的applicationUrl
条目指定的端口号相匹配。以下是launchSettings.json
文件的内容:
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:30959",
"sslPort": 44345
}
},
"profiles": {
"WebApplication1": {
"commandName": "Project",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:7139;http://localhost:5139",
"dotnetRunMessages": true
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
为什么这个还是不起作用呢?是因为我试图使用Windows身份验证使其工作,还是我在做错了什么?
"/api/*"
,但目前还没有成功。我会继续探索。 - Sylvain C./api
,而不是/api/*
,你也试过了吗? - Chris