2

我在服务器端和客户端都使用 BreezeJs。我有以下控制器动作。当找不到产品代码时,我希望获得 404 http 代码。

public Product GetProduct(string code)
    {
        var p = _contextProvider.Context.Products.Where(p => p.Code == code).FirstOrDefault();
        if (p == null)
        {
            //does not work because because breeze swallows the exception
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
        return p;    
    }

以下是回应。HttpResponseException 被 BreezeApi 吞噬。有任何想法吗?先感谢您。

{

    "$id": "1",
    "$type": "System.Web.Http.HttpError, System.Web.Http",
    "Message": "An error has occurred.",
    "ExceptionMessage": "Cannot perform runtime binding on a null reference",
    "ExceptionType": "Microsoft.CSharp.RuntimeBinder.RuntimeBinderException",
    "StackTrace": " at CallSite.Target(Closure , CallSite , Object )\r\n at System.Dynamic.UpdateDelegates.UpdateAndExecute1[T0,TRet](CallSite site, T0 arg0)\r\n at Breeze.WebApi.ODataActionFilter.OnActionExecuted(HttpActionExecutedContext actionExecutedContext)\r\n at System.Web.Http.Filters.ActionFilterAttribute.CallOnActionExecuted(HttpActionContext actionContext, HttpResponseMessage response, Exception exception)\r\n at System.Web.Http.Filters.ActionFilterAttribute.<>c__DisplayClass2.<System.Web.Http.Filters.IActionFilter.ExecuteActionFilterAsync>b__0(HttpResponseMessage response)\r\n at System.Threading.Tasks.TaskHelpersExtensions.<>c__DisplayClass41`2.<Then>b__40(Task`1 t)\r\n at System.Threading.Tasks.TaskHelpersExtensions.ThenImpl[TTask,TOuterResult](TTask task, Func`2 continuation, CancellationToken cancellationToken, Boolean runSynchronously)"

}
4

2 回答 2

2

Breeze 使用的操作过滤器不是为处理NULL内容值而设计的。

如果您以这种方式生成未找到响应,则可以解决它,请注意此未找到响应中有一些内容,即字符串消息。

public HttpResponseMessage  GetProduct(string code)
{
    var p = _contextProvider.Context.Products.Where(p => p.Code == code).FirstOrDefault();
    if (p == null)
    {
       Request.CreateErrorResponse(HttpStatusCode.NotFound, "Couldn't find the resource");
    }
    Request.CreateResponse(HttpStatusCode.OK, p);
}

我认为这是微风中的一个错误。

!actionExecutedContext.Response.IsSuccessStatusCodeBreeze的代码可能应该被更改为TryGetContentValue不尝试解析错误响应()。

public class ODataActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
        base.OnActionExecuting(actionContext);
    }
    /// <summary>
    /// Called when the action is executed.
    /// </summary>
    /// <param name="actionExecutedContext">The action executed context.</param>
    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        if (actionExecutedContext.Response == null)
        {
            return;
        }

        if(!actionExecutedContext.Response.IsSuccessStatusCode)
        {
            return;
        }

和/或至少在这里进行一个简单的 Null 引用检查:

public class ODataActionFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
        base.OnActionExecuting(actionContext);
    }
    /// <summary>
    /// Called when the action is executed.
    /// </summary>
    /// <param name="actionExecutedContext">The action executed context.</param>
    public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
        if (actionExecutedContext.Response == null)
        {
            return;
        }

        object responseObject;
        if(!actionExecutedContext.Response.TryGetContentValue(out responseObject))
        {
            return;
        }
于 2012-11-21T08:59:26.840 回答
2

现在应该在微风 0.73.1 中修复此问题。即抛出一个 HttpResponseException 现在会将该异常返回给客户端。

于 2012-11-22T02:48:12.310 回答