首页 > 解决方案 > 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));
  }

标签: c#angularasp.net-web-api2

解决方案


推荐阅读