Odata异常从服务器传递到客户端。

问题描述:





我正在尝试找到将异常或消息从我的Odata服务传递回Odata网络表单客户端的最佳方法。



在服务上,当我给这个控制器打电话时:



公共异步任务< ihttpactionresult>发布(产品)

{



var message =找不到ID的产品;

抛出新的HttpResponseException(

Request.CreateErrorResponse(HttpStatusCode.NotImplemented,message));

}



It按预期在内容中抛出带有HttpError的HttpResponse exeption。



但是,在客户端,异常作为DataServiceRequestException或DataServiceQueryException以及内部异常消息到达格式如下:





{

odata.error:{

代码:,消息:{

lang:en-US,value:产品ID =未找到

}

}

}



那么,如何从JSON中轻松提取消息?



这是最好的方法吗?



我应该只返回一个响应代码,把消息放在h读者?



有没有人有一个错误管理的webforms客户端的例子,他们可以指点我好吗?



我花了好几个小时就不知道该做什么。



谢谢

杰夫

Hi,

I am trying to find the best way to pass exceptions or messages from my Odata Service back to an Odata web forms client.

On the service, when I call this controller:

public async Task<ihttpactionresult> Post(Products product)
{

var message = "Product with id = not found";
throw new HttpResponseException(
Request.CreateErrorResponse(HttpStatusCode.NotImplemented, message));
}

It throws a HttpResponse exeption with the HttpError in the content as expected.

However, on the client side, the exception arrives as either a DataServiceRequestException or DataServiceQueryException and the inner exception message is formatted as follows:


{
"odata.error":{
"code":"","message":{
"lang":"en-US","value":"Product with id = not found"
}
}
}

So, how do I easily extract the message from the JSON?

Is this the best way to do it?

Should I just return a response code and put the message in the headers?

Does anyone have an example of a webforms client with error management that they can point me to please?

I have spent hours on this and don't know what to do.

Thanks
Jeff

我还在努力解决这个问题。有人有提示吗?



谢谢。
I'm still struggling with this. Does anyone have a hint please?

Thanks.