Controllers.cs 适用于不同的地址 - 来自 Visual Studio 模板的 .net core Angular 应用程序

Xia*_*Xia 5 get http asp.net-core angular

我从 Visual Studio 模板在 .Net Core 和 Agnular 中创建了一个新项目,但无法从控制器获取数据。它适用于从模板 WeatherController.cs 生成的控制器,但不适用于我自己的控制器。我的意思是当我这样调用控制器时它会起作用:

https://localhost:5001/home/getData/1

但应用程序从 5002 开始,weatherController 中的方法也适用于 5002。我不明白为什么一个控制器在 5002 上工作,另一个在 5001 上工作。任何人都可以解释我如何正确配置它,或者向我发送一些源代码以了解这是如何进行的作品。谢谢

配置

    {
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:49323",
      "sslPort": 44374
    }
  },
  "profiles": {
    "Server": {
      "commandName": "Project",
      "launchBrowser": true,
      "launchUrl": "https://localhost:5002",
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
      }
    },
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development",
        "ASPNETCORE_HOSTINGSTARTUPASSEMBLIES": "Microsoft.AspNetCore.SpaProxy"
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

控制器

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

    [HttpGet]
    [Route("GetData/{id}")]
    public ActionResult<string> GetData(int id)
    {
        return "ok";
    }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

服务.ts

//baseURL - https://localhost:5002

return this.http.get<any>(this.baseURL + 'home/GetData/' + id)
      .pipe(
        retry(1),
        catchError(this.errorHandler)
      );


http.get<WeatherForecast[]>(baseUrl + 'weatherforecast').subscribe(result => {
  this.forecasts = result;
}, error => console.error(error));
Run Code Online (Sandbox Code Playgroud)

第一个仅当我将 5002 更改为 5001 时才起作用,但第二个仅在我将 5002 更改为 5002 时才起作用...

https://localhost:5001/home/getData/1 - 好的

https://localhost:5002/home/getData/1 - 不行

https://localhost:5002/weatherforecast - 好的

Fir*_*zar 5

尝试包含文件/home路径ClientApp\proxy.conf.js

const PROXY_CONFIG = [
  {
    context: [
      "/weatherforecast",
      "/home"
   ],
    target: "https://localhost:5001",
    secure: false
  }
]

module.exports = PROXY_CONFIG;
Run Code Online (Sandbox Code Playgroud)

这是代理的角度配置