WCF 数据服务中的自定义错误响应
在我的 DataService 派生类中,我重写了 HandleException 方法,以便进行自定义异常处理(简化示例):
protected override void HandleException(HandleExceptionArgs args)
{
args.Exception = new DataServiceException(
args.ResponseStatusCode,
"RecordAlreadyExist",
"The record with the given ID already exists in the database",
System.Globalization.CultureInfo.CurrentCulture.Name,
null);
base.HandleException(args);
}
这将导致类似于以下 JSON 响应的结果:
{
"error": {
"code": "RecordAlreadyExist",
"Message": {
"Language": "en-US",
"Message": "The record with the given ID already exists in the database"
}
}
}
但我想在错误响应中提供一些附加信息,例如
{
"error": {
"code": "RecordAlreadyExist",
"Message": {
"Language": "en-US",
"Message": "The record with the given ID already exists in the database"
},
"RecordID": "1234"
}
}
:我可以这样做吗? DataServiceException 仅支持错误代码和消息...
In my DataService derrived class I have overriden the HandleException method in order to do custom exception handling (simplified example):
protected override void HandleException(HandleExceptionArgs args)
{
args.Exception = new DataServiceException(
args.ResponseStatusCode,
"RecordAlreadyExist",
"The record with the given ID already exists in the database",
System.Globalization.CultureInfo.CurrentCulture.Name,
null);
base.HandleException(args);
}
This would result into something like the following JSON response:
{
"error": {
"code": "RecordAlreadyExist",
"Message": {
"Language": "en-US",
"Message": "The record with the given ID already exists in the database"
}
}
}
But I would like to have some additional information in the error response like:
{
"error": {
"code": "RecordAlreadyExist",
"Message": {
"Language": "en-US",
"Message": "The record with the given ID already exists in the database"
},
"RecordID": "1234"
}
}
How can I do that? DataServiceException only supports an error code and a message...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论