使用WebApi2和Angular,指定不返回数据? (也没有报错)
Using Web Api 2 and Angular, specify that no data is returned? (No error either)
假设我的 API returns 为简单起见查看产品的日期列表。一些产品从未被查看过,将返回一个空列表。发生这种情况时,我想显示:
This product hasn't been viewed yet!
并隐藏带有产品查看日期的 table。我在返回 IHttpActionResult
:
的控制器中执行此操作
if (itemViewedDto.Count > 0)
{
return Ok(itemViewedDto);
}
else
{
return Ok(new {message = "No data"});
}
在我的客户端上(itemViewed 只是 http 响应负载):
$scope.dataExists = function(){
if($scope.itemViewed.hasOwnProperty(message)){
return true;
} else {
return false;
}
}
然后使用 ng-if 或其他任何方式相应地显示响应以查看是否有响应?
这有什么问题吗?这是一个公认的模式吗?如果没有,什么是更好的方法呢?我是从概念上问的,不是针对这种特殊情况。
目前它对我有用,只是我很想知道它是否有误。
就我个人而言,我会避免在成功的 API 呼叫中 return 两种不同类型的响应。它看起来很臭,而且完全没有用。只是 return 一个列表,即使它是一个空列表。在 Angular 方面,只需检查数组是否为空,然后根据该数组执行显示逻辑。
使用正确的 HTTP 状态代码和 return 204 无内容。 WebApi 不提供 "NoContent" 结果,但您可以使用 "NoContentResult" class 非常轻松地提供结果。这样您就可以坚持使用 IHttpActionResults 而不会退回到 HttpResponseMessages。
public class NoContentResult : OkResult
{
public NoContentResult(ApiController controller) : base(controller) { }
public override Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var response = base.ExecuteAsync(cancellationToken).Result;
response.StatusCode = HttpStatusCode.NoContent;
return Task.FromResult(response);
}
}
现在使用它,您的状态代码将设置为空体:
if (itemViewedDto.Count > 0)
{
return Ok(itemViewedDto);
}
else
{
return new NoContentResult(this);
}
如果您的所有控制器都使用一个底座 class,您可以将其添加到您的底座 class,然后它就像一个 Ok 响应。
public abstract class BaseApiController : ApiController
{
/// <summary>
/// Returns a No Content response.
/// </summary>
protected NoContentResult NoContent()
{
return new NoContentResult(this);
}
}
现在你可以这样使用了:
if (itemViewedDto.Count > 0)
{
return Ok(itemViewedDto);
}
else
{
return NoContent();
}
现在,在您的 JavaScript 中,您可以响应不同的状态代码,而不是解析正文。
假设我的 API returns 为简单起见查看产品的日期列表。一些产品从未被查看过,将返回一个空列表。发生这种情况时,我想显示:
This product hasn't been viewed yet!
并隐藏带有产品查看日期的 table。我在返回 IHttpActionResult
:
if (itemViewedDto.Count > 0)
{
return Ok(itemViewedDto);
}
else
{
return Ok(new {message = "No data"});
}
在我的客户端上(itemViewed 只是 http 响应负载):
$scope.dataExists = function(){
if($scope.itemViewed.hasOwnProperty(message)){
return true;
} else {
return false;
}
}
然后使用 ng-if 或其他任何方式相应地显示响应以查看是否有响应?
这有什么问题吗?这是一个公认的模式吗?如果没有,什么是更好的方法呢?我是从概念上问的,不是针对这种特殊情况。
目前它对我有用,只是我很想知道它是否有误。
就我个人而言,我会避免在成功的 API 呼叫中 return 两种不同类型的响应。它看起来很臭,而且完全没有用。只是 return 一个列表,即使它是一个空列表。在 Angular 方面,只需检查数组是否为空,然后根据该数组执行显示逻辑。
使用正确的 HTTP 状态代码和 return 204 无内容。 WebApi 不提供 "NoContent" 结果,但您可以使用 "NoContentResult" class 非常轻松地提供结果。这样您就可以坚持使用 IHttpActionResults 而不会退回到 HttpResponseMessages。
public class NoContentResult : OkResult
{
public NoContentResult(ApiController controller) : base(controller) { }
public override Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
var response = base.ExecuteAsync(cancellationToken).Result;
response.StatusCode = HttpStatusCode.NoContent;
return Task.FromResult(response);
}
}
现在使用它,您的状态代码将设置为空体:
if (itemViewedDto.Count > 0)
{
return Ok(itemViewedDto);
}
else
{
return new NoContentResult(this);
}
如果您的所有控制器都使用一个底座 class,您可以将其添加到您的底座 class,然后它就像一个 Ok 响应。
public abstract class BaseApiController : ApiController
{
/// <summary>
/// Returns a No Content response.
/// </summary>
protected NoContentResult NoContent()
{
return new NoContentResult(this);
}
}
现在你可以这样使用了:
if (itemViewedDto.Count > 0)
{
return Ok(itemViewedDto);
}
else
{
return NoContent();
}
现在,在您的 JavaScript 中,您可以响应不同的状态代码,而不是解析正文。