【发布时间】:2018-08-28 07:33:21
【问题描述】:
我有这个控制器:
namespace Cantrel.Application.CantrelSearchApi.Controllers
{
[Route("api/[controller]")]
[Authorize]
public class MonitorsController : Controller
{
private readonly IMonitoringApiService monitoringService;
private readonly IClientsApiService clientsService;
private readonly ILogger<MonitorsController> logger;
public MonitorsController(IMonitoringApiService monitoringService,
IClientsApiService clientsService,
ILogger<MonitorsController> logger)
{
this.monitoringService = monitoringService;
this.clientsService = clientsService;
this.logger = logger;
}
[HttpGet("{id}")]
public async Task<IActionResult> Get(string id)
{
if (string.IsNullOrEmpty(null))
{
return BadRequest("No id was provided. Please provide a valid monitor id or subject id.");
}
try
{
MonitorDto monitor;
if (Guid.TryParse(id, out Guid monitorId))
{
monitor = await GetByMonitorId(monitorId);
}
else
{
monitor = await GetBySubjectId(id);
}
if (monitor == null)
{
return NotFound();
}
return Json(monitor);
}
catch (Exception ex)
{
logger.LogError($"[{Request.Path.Value}]: {ex.ToString()}");
return new StatusCodeResult(500);
}
}
我在 Postman 中进行以下调用:
GET: {{api}}/Monitors/ABCDEFGH-1234-4567-8901-ABCDEFGHIJKL
我收到了400 Bad Request 错误消息No id was provided. Please provide a valid monitor id or subject id,而不是预期的信息返回。
我不确定我是否理解这是怎么发生的。该字符串显然既不是 NULL 也不是空的。
如果我应该提供任何其他类的代码来帮助诊断,请告诉我。
【问题讨论】:
标签: c# asp.net-mvc asp.net-web-api