使用.NET核心路由的Angular应用程序不工作

nnsrf1az  于 2023-02-26  发布在  .NET
关注(0)|答案(2)|浏览(166)
    • bounty将在4天后过期**。回答此问题可获得+500声望奖励。muttley91希望引起更多人关注此问题。

我使用this guide创建了一个ASP.NET核心应用程序,它可以按原样工作,但是当我尝试向API添加一个新的控制器时,它不允许我路由到那个控制器操作。
我这里有回购协议https://github.com/rarDevelopment/rardk-web-dotnet/tree/main/rardk.web

    • 关键信息:**

localhost:4200Angular 应用程序
localhost:7042
API

它们应该是使用proxy.conf.js进行代理的,但是如果我将该文件完全注解掉,它仍然适用于WeatherForecastController。

    • 以下是说明问题的场景(尝试通过Postman,但Angular应用程序也在app.component.ts中调用这些端点):**

https://localhost:4200/weatherforecast-〉这是可行的
https://localhost:7042/weatherforecast-〉这是可行的
https://localhost:4200/api/now/letterboxd-〉这起作用
https://localhost:7042/api/now/letterboxd-〉这确实有效

    • 问题**:如果修改proxy.conf.js没有任何作用(因为我也尝试过添加新路由),那么是什么使得weatherforecast端点在端口4200上工作,而其他任何东西都不起作用?

编辑:添加代码片段来说明问题:
现控制人:

using Microsoft.AspNetCore.Mvc;
using rardk.web.BusinessLayer;

namespace rardk.web.API.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class NowController : ControllerBase
    {
        private readonly ILetterboxdBusinessLayer _letterboxdBusinessLayer;

        public NowController(ILetterboxdBusinessLayer letterboxdBusinessLayer)
        {
            _letterboxdBusinessLayer = letterboxdBusinessLayer;
        }

        [HttpGet("letterboxd", Name = "letterboxd")]
        public ActionResult GetLetterboxdFeed()
        {
            _letterboxdBusinessLayer.GetLetterboxdFeed();
            return Ok();
        }
    }
}

天气预报员(本工程):

using Microsoft.AspNetCore.Mvc;

namespace rardk.web.API.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet(Name = "GetWeatherForecast")]
        public IEnumerable<WeatherForecast> Get()
        {
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}

proxy.conf.js(似乎不执行任何操作):

const PROXY_CONFIG = [
  {
    context: [
      "/test"
    ],
    target: "https://localhost:7042",
    secure: false
  }
]

module.exports = PROXY_CONFIG;
8hhllhi2

8hhllhi21#

您必须理解proxy.conf.js文件。
特别是context键,从你提到的例子中,你已经将https://localhost:4200/test代理到https://localhost:7042/test,就这样。

bxjv4tth

bxjv4tth2#

我认为您缺少API的代理,您设置的配置没有代理API端点
这是我使用的

const { env } = require('process');

const target = env.ASPNETCORE_HTTPS_PORT ? `https://localhost:${env.ASPNETCORE_HTTPS_PORT}` :
  env.ASPNETCORE_URLS ? env.ASPNETCORE_URLS.split(';')[0] : 'https://localhost:5001';

const PROXY_CONFIG = [
  {
    context: [
      "/weatherforecast",
      "/_configuration",
      "/.well-known",
      "/Identity",
      "/connect",
      "/ApplyDatabaseMigrations",
      "/_framework",
      "/api",
      "/favicon.ico"
   ],
    target: target,
    secure: false,
    headers: {
      Connection: 'Keep-Alive'
    }
  }
]

module.exports = PROXY_CONFIG;

通过代理,API端点也应该能够访问此
https://本地主机:4200/应用程序接口/现在/信箱d

相关问题