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