我在服務器和客戶端都使用BreezeJs。我有以下控制器操作。如果找不到產品代碼,我希望獲得404 http代碼。在Breezejs中啓用webapi控制器拋出HttpResponseException
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)"
}
@ChauHoMan - 現在應該修復。如果傑的回答(和修正)讓你滿意,請點擊上/下的勾號表示你接受它。如果沒有,請告訴我們,我們會繼續挖掘。 Thx – Ward
@簽入的修復程序與我推薦的解決方案和診斷完全相同 - 沒有任何歸屬或參考信息回到此問題。 –
對不起,馬克!只是爭先恐後地趕上所有的要求。你的診斷和解決方案是正確的。我們感謝它! –