c# - False Boolean 作为空对象返回给 Angular
问题描述
我有一个返回 true 或 false 值的 C# API 调用。当它返回 false 值时,它作为一个空对象返回,但只返回给 Angular。当我在 REST 客户端中使用相同的值测试相同的调用时,我得到了我期望的错误值。
有没有理由让我得到一个空对象来代替错误的结果?我已经检查了从 Angular 应用程序(它们是)正确传递给 API 的值,并且我在 C# 方法中添加了一个断点,以检查它离开 API 时肯定是错误的结果和不是空的对象。
C#方法如下:
// Check if trailblazer is on journey
[Route("Journey/TrailblazerRegistered/{journeyId}/{username}")]
[HttpGet]
public IHttpActionResult IsTrailblazerRegistered(string journeyId, string username)
{
// First we assume the user is not registered
bool UserRegistered = false;
// Make sure we have a guid and a user
if (journeyId == null || username == null || username == "") return NotFound();
// If we do then get the user guid
User UserDb = WmData.Users.FirstOrDefault(u => u.Username == username);
// Check we've got a user and if we have get the GUIDs
if (UserDb == null) return NotFound();
Guid UserGuid = UserDb.UserId;
Guid JourneyGuid = new Guid(journeyId);
// List of Trailblazers for this Journey
List<UserJourney> Trailblazers = WmData.UserJourneys.Where(j => j.JourneyId == JourneyGuid).ToList();
// See if we can find our user in that list
UserJourney RegisteredUser = Trailblazers.Find(u => u.UserId == UserGuid);
// Did we find a user?
if (RegisteredUser != null) UserRegistered = true;
// Return true / false
return Ok(UserRegistered);
}
来自 Angular 的 API 调用是:
// Check if Trailblazer is already on Journey
userRegisteredToJourney() {
this._wmapi
.getService("Journey/TrailblazerRegistered/" + this.id + "/" + this._wmapi.tempuser)
.then((result) => {
this.userSignedToJourney = result;
console.log("Journey/TrailblazerRegistered/" + this.id + "/" + this._wmapi.tempuser);
})
.catch(error => console.log(error));
}
解决方案
推荐阅读
- sql - Postgres 获取数据时间间隔
- ios - 如何确保我的标签始终位于该行的中心?
- json - 是否有可能在 Golang 中拥有动态键的结构以及 json 的静态键
- java - Android:如何在视图的可见性更改时触发事件?
- python - 函数返回值 if in DB else Continue
- python - 网页抓取时在循环中跳过错误
- javascript - Express.js 中的密码散列问题
- python - 浮点数的算术运算给出了意想不到的结果
- json - Jolt - 如何将两个数组转换为键值并保留其他单打属性
- json - 从 JSON 中删除空值和/或 null 值