开发者

Where to catch exceptions from customized ModelBinder

开发者 https://www.devze.com 2022-12-13 21:26 出处:网络
I have a customized ModelBinder whi开发者_如何学运维ch bind web from with a object using code like this\"

I have a customized ModelBinder whi开发者_如何学运维ch bind web from with a object using code like this"

    [ModelBinder(typeof(CustomizedModelBinder))]
    public class Widget{ ... }

This modelbinder may throw exceptions and where should I add code to catch those exceptions? Thanks in advance!


From design perspective it is better for a model binder to add model errors instead of throwing exceptions:

ModelState.AddModelError("Phone", "Phone number is invalid.");

This way, later in your action you could check if the model is valid:

if (!ModelState.IsValid)
{
    ...
}
0

精彩评论

暂无评论...
验证码 换一张
取 消