Azure Web API 在部署但在本地工作时给出 404
Azure Web API giving a 404 when deployed but works locally
我们有一个 .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
我们看到了这些类似的帖子,但他们没有解决这个问题:
- Web API interface works locally but gets 404 after deployed to Azure Website
我希望我能提供更多见解,但我们真的不知道这里会发生什么。任何想法将不胜感激。
您可以使用Self-Contained
模式部署您的web项目,然后找到project_name.exe和double-click它。在本地测试,你的测试 url 应该是 https://localhost:5001/api/Bin/1234/TestBin
.
如果你可以 运行 它在你的本地运行良好,第二步你应该创建一个新的 webapp,然后像往常一样部署它。我们只是在您原来的 webapp 中找出了一些特定的原因。(例如:部署失败)
如果仍然不行,我的建议是您可以手动将发布文件拖放到 azure webapp 的 kudu 站点。
上面的步骤应该对你有用,不过我觉得最简单的方法还是去kudu网站获取dll文件,然后反编译,这样才能找到问题的根源。
这没有任何意义,但我只是将控制器的名称从“BinController”更改为“BinsController”,现在它可以工作了...
不能将控制器命名为“Bin”。
我们有一个 .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
我们看到了这些类似的帖子,但他们没有解决这个问题:
- Web API interface works locally but gets 404 after deployed to Azure Website
我希望我能提供更多见解,但我们真的不知道这里会发生什么。任何想法将不胜感激。
您可以使用Self-Contained
模式部署您的web项目,然后找到project_name.exe和double-click它。在本地测试,你的测试 url 应该是 https://localhost:5001/api/Bin/1234/TestBin
.
如果你可以 运行 它在你的本地运行良好,第二步你应该创建一个新的 webapp,然后像往常一样部署它。我们只是在您原来的 webapp 中找出了一些特定的原因。(例如:部署失败)
如果仍然不行,我的建议是您可以手动将发布文件拖放到 azure webapp 的 kudu 站点。
上面的步骤应该对你有用,不过我觉得最简单的方法还是去kudu网站获取dll文件,然后反编译,这样才能找到问题的根源。
这没有任何意义,但我只是将控制器的名称从“BinController”更改为“BinsController”,现在它可以工作了...
不能将控制器命名为“Bin”。