【问题标题】:Azure Web API giving a 404 when deployed but works locallyAzure Web API 在部署时提供 404 但在本地工作
【发布时间】:2022-05-06 05:22:14
【问题描述】:

我们将 .NET Core Web API 部署为 Azure Web 应用程序。所有端点都在本地工作,但是,一旦部署,我们就有一个控制器,它为我们在其中命中的所有端点提供 404。

我们已经检查并三次检查了我们调用的 url 是否正确,据我们所知,这个控制器与我们应用程序中的其他控制器没有什么不同。

这是给我们 404 的 BinController:

namespace API.Controllers
{
    [Route("api/[controller]")]
    [Authorize]
    [ApiController]
    public class BinController : ControllerBase
    {
        private readonly IBinRepository _binRepo;
        private readonly ILogger _logger;

        public BinController(IBinRepository binRepo, ILogger<BinController> logger)
        {
            _binRepo = binRepo;
            _logger = logger;
        }

        [HttpGet("{locationId}/{binId}")]
        public async Task<IActionResult> CheckBinExists(int locationId, string binId)
        {
            try
            {
                bool result = await _binRepo.CheckBinExists(locationId, binId);
                return Ok(result);
            }
            catch (Exception e)
            {
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }
        }

        [HttpGet("findAll/{locationId}/{itemId}")]
        public async Task<IActionResult> FindAllBinsWithItem(int locationId, string itemId)
        {
            try
            {
                var result = await _binRepo.FindAllBinsWithItem(locationId, itemId);
                return Ok(result);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }
        }

        [HttpGet("contents/{locationId}/{bin}")]
        public async Task<IActionResult> GetBinContents(int locationId, string bin)
        {
            try
            {
                List<BatchLine> contents = await _binRepo.GetBinContents(locationId, bin);
                return Ok(contents);
            }
            catch (Exception e)
            {
                _logger.LogError(e.Message);
                return StatusCode(StatusCodes.Status500InternalServerError, e.Message);
            }
        }
    }
}

我们打电话给https://ourapiname.azurewebsites.net/api/Bin/1234/TestBin

总结:

  • 所有端点都在本地工作
  • 所有控制器在部署时都能正常工作,除了一个
  • 我们的应用程序中有多个其他控制器具有类似的设置(如果不一样的话),但是这个在部署时返回 404

我们看到了这些类似的帖子,但他们没有解决这个问题:

我希望我能提供更多的见解,但我们真的对这里可能发生的事情感到茫然。任何想法将不胜感激。

【问题讨论】:

  • 这个有日志吗?就像在应用程序洞察或内部日志记录中一样。

标签: azure asp.net-core azure-web-app-service


【解决方案1】:

您可以使用Self-Contained 模式部署您的Web 项目,然后找到project_name.exe 并双击它。在本地测试一下,你的测试地址应该是https://localhost:5001/api/Bin/1234/TestBin

如果你可以在本地运行它,你应该在第二步创建一个新的 webapp,然后像往常一样部署它。我们只是在您的原始 webapp 中排除了某些特定原因。(例如:部署失败)

如果还是不行,我的建议是你可以手动将发布文件拖放到azure webapp的kudu站点。


上面的步骤应该对你有用,但是我觉得最简单的方法是去kudu站点获取dll文件,然后反编译,这样才能找到问题的根源。

【讨论】:

    【解决方案2】:

    这没有任何意义,但我只是将控制器的名称从“BinController”更改为“BinsController”,现在它可以工作了......

    不能将控制器命名为“Bin”。

    【讨论】:

      猜你喜欢
      • 2016-06-20
      • 2012-11-05
      • 1970-01-01
      • 2022-07-17
      • 1970-01-01
      • 2012-09-17
      • 2013-01-05
      • 2023-01-15
      • 1970-01-01
      相关资源
      最近更新 更多