2

サーバー側とクライアント側の両方でBreezeJを使用しています。次のコントローラーアクションがあります。製品コードが見つからない場合に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

これは、breath 0.73.1 で修正されるはずです。つまり、HttpResponseException をスローすると、その例外がクライアントに返されます。

于 2012-11-22T02:48:12.310 に答える
2

Breeze が使用しているアクション フィルターは、NULL Content 値を処理するようには設計されていません。

この方法で not found 応答を生成すると、回避できます。この Not Found 応答には、文字列メッセージなどのコンテンツがあることに注意してください。

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);
}

これは Breeze のバグだと思います。

Breeze のコードは、失敗した応答 ( ) を解析しようとしないように変更するか、コンテンツの取得に失敗した場合にメソッド!actionExecutedContext.Response.IsSuccessStatusCodeを適切に使用しTryGetContentValueてエスケープするように変更する必要があります (現状では、コンテンツの取得に失敗した場合に返される false は無視されます)。 )。

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 に答える