False布尔值作为空对象返回Angular

时间:2018-09-03 13:14:48

标签: c# angular asp.net-web-api2

我有一个C#API调用,它返回一个true或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));
  }

0 个答案:

没有答案