参数绑定失败时的自定义错误

时间:2013-06-13 17:46:53

标签: c# asp.net-mvc

通常在这样的网络API操作上:

[HttpGet]
public async Task<Item> Get(Guid ItemId){...}

如果HTTP请求不包含ItemId参数,它将返回400 Bad Request,其中包含

等异常
The parameters dictionary contains a null entry for parameter 'ItemId' of non-nullable type...

有没有办法在发生这种情况时提供自定义错误消息,而不是异常消息而不将ItemId转换为可空类型

1 个答案:

答案 0 :(得分:0)

您可以编写自定义过滤器属性,根据需要处理并返回正确的响应和消息。

Here is a great article on how to do so.

相关问题