【发布时间】:2020-11-27 22:00:10
【问题描述】:
我已经定义了一个可以为空的 Guid 参数的操作,如下所示:
var searchLocationAction = builder.EntityType<SearchDummyDTO>().Collection.Action("GetLocation").Returns<SearchResultDTO>();
searchLocationAction.Parameter<string>("searchPhrase");
searchLocationAction.Parameter<Guid?>("areaId");
控制器方法看起来像
[HttpPost]
public async Task<IActionResult> GetLocation(ODataActionParameters parameters)
在 API 使用者使用错误正文发出请求的情况下:
{"searchPhrase":"","areaId":""}
将引发异常而不向用户提供任何反馈。同样在开发模式下,我们会得到一个参数空异常:
<h1>An unhandled exception occurred while processing the request.</h1>
<div class="titleerror">ArgumentNullException: Value cannot be null.<br />
Parameter name: source</div>
<p class="location">System.Linq.Enumerable.ToList<TSource>(IEnumerable<TSource> source)</p>
我正在尝试找到一种解决方案,至少让消费者知道请求正文存在问题。
我尝试的是创建一个自定义中间件来捕获 ODataException,但这没有作为 odata 异常引发,这是无用的。
【问题讨论】:
标签: asp.net-core odata