在Breezejs启用webapi控制器中抛出HttpResponseException

时间:2012-11-21 06:39:01

标签: asp.net-web-api breeze

我在服务器端和客户端都使用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;    
    }

以下是回复。 BreezeApi吞噬了HttpResponseException。有任何想法吗?提前谢谢。

{

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

}

2 个答案:

答案 0 :(得分:2)

action filter Breeze is using并非旨在处理NULL内容值。

如果以这种方式生成未找到的响应,则可以解决此问题。请注意,此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中的一个错误。

Breeze的代码可能应该更改为不尝试解析出现故障的响应(!actionExecutedContext.Response.IsSuccessStatusCode)或正确使用其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;
        }

答案 1 :(得分:2)

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