Asp.net web api 在启用Breezejs的webapi控制器中抛出HttpResponseException

Asp.net web api 在启用Breezejs的webapi控制器中抛出HttpResponseException,asp.net-web-api,breeze,Asp.net Web Api,Breeze,我在服务器端和客户端都使用BreezeJs。我有以下控制器操作。我希望在找不到产品代码时获得404HTTP代码 public Product GetProduct(string code) { var p = _contextProvider.Context.Products.Where(p => p.Code == code).FirstOrDefault(); if (p == null) { //does

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

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)"

}
未设计用于处理空内容值

如果以这种方式生成not found响应,则可以解决此问题,请注意,此not found repsonse中有一些内容,即字符串消息

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的代码可能应该修改为不尝试解析错误响应!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;
        }
和/或至少在此处进行简单的空引用检查:

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;
        }
未设计用于处理空内容值

如果以这种方式生成not found响应,则可以解决此问题,请注意,此not found repsonse中有一些内容,即字符串消息

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的代码可能应该修改为不尝试解析错误响应!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;
        }
和/或至少在此处进行简单的空引用检查:

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

现在应在微风0.73.1中修复此问题。i、 e.抛出HttpResponseException现在将向客户端返回该异常

现在应在微风0.73.1中修复此问题。i、 e.抛出HttpResponseException现在将向客户端返回该异常

@ChauHoMan-现在应该可以修复了。如果Jay的回答和修复令您满意,请单击“向上/向下”下方的勾号表示您接受。如果没有,请告诉我们,我们将继续挖掘。Thx@Ward签入的修复程序与我推荐的解决方案和诊断完全相同-没有此问题的归属或引用。抱歉,Mark!只是为了赶上所有的请求。您的诊断和解决方案正确无误。我们很感激@JayTraband别担心,这么快就解决了,做得很好。@ChauHoMan-现在应该解决了。如果Jay的回答和修复令您满意,请单击“向上/向下”下方的勾号表示您接受。如果没有,请告诉我们,我们将继续挖掘。Thx@Ward签入的修复程序与我推荐的解决方案和诊断完全相同-没有此问题的归属或引用。抱歉,Mark!只是为了赶上所有的请求。您的诊断和解决方案正确无误。我们很感激@JayTraband不用担心,很好地解决了这么快的问题。