简体   繁体   English

ASP.NET MVC中异常控制流的替代方法

[英]Alternative To Exception Control Flow in ASP.NET MVC

I have read that exception control flow is a bad idea. 我读过异常控制流是个坏主意。

if(products.length == 0)
   throw new CustomException("Product is required");

I am using ajax at the moment for my requests and if a 500 error happens I catch them in an Exception filter that returns the exception in a strongly typed object that is sent to the client in JSON to be shown in a popup. 我目前正在使用ajax来处理请求,如果发生500错误,我会在Exception过滤器中捕获到它们,该过滤器在强类型对象中返回异常,该对象以JSON的形式发送到客户端,并在弹出窗口中显示。 In an ASP.NET MVC applicacation what is the better alternative and an example? 在ASP.NET MVC应用程序中,更好的替代方案和示例是什么?

如果这是出于模型验证的目的,则最好使用数据注释等标准技术,或者,如果您像我一样,发现它们在复杂的验证场景中难以使用,则还可以签出FluentValidation.NET

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM