模型状态错误(或)特定类型的自定义异常 [英] Model State Errors (or) Custom exceptions of a certain type

查看:119
本文介绍了模型状态错误(或)特定类型的自定义异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个带有JQUERY应用程序的ASP.NET MVC应用程序会抛出4种错误



Unhanded exception(400,403,500 503等) - 抛出一般异常

NOT SEVERE预期异常/错误(自定义异常) - 例如试图寻找系统中不存在的联系人 - 系统需要抛出Contact_not_found_exception并将其显示给用户,因为系统中不存在联系人。它不会阻止用户做任何其他事情,只是为了提醒用户

非常严重预期的异常/错误(自定义异常) - 例如尝试创建系统中已存在的联系人 - 系统需要抛出Contact_duplicated_exception并向用户显示之前已创建联系人。无法继续,因为名称应该是唯一的

模型状态错误(UI)。我使用@Html.ValidationSummary(true)添加到模型状态以显示在页面上的错误



捕获前端捕获的客户端验证错误并捕获应用程序错误在global.asax



我正在寻找一种方法来捕获类型2和3的错误/异常并将其返回给AJAX()具有一定严重性的调用键入严重与警告。



我一直在关注处理Asp.net MVC中Ajax调用验证错误的链接



http://erraticdev.blogspot.com/2010/11/handling-validation-errors-on-ajax.html



我无法概括并将所有模型状态错误添加为ModelStateException。由于大多数这些错误/异常属于某种严重性类型,我的前端(JQUERY)应该读取此类错误并执行不同的操作。



1.How创建自定义异常字典,严重性类型? 2.如何抛出这些并将其添加到模型状态错误中。 3.如何使用Custom Handle Erro Arrtibute和IExceptionFilter全局捕获这些



谢谢,

Hi All,

I have an application ASP.NET MVC with JQUERY application that throws 4 kinds of errors

Unhanded exceptions (400, 403,500 503 etc) - throw generic exceptions
NOT SEVERE Expected exceptions/ errors (custom exceptions)- e.g. Trying to look for a contact that does not exist in the system - The system needs to throw "Contact_not_found_exception" and show this to the user as "Contact does not exist in the system" . It does not stop the user from doing anything else but its just to alert the user
VERY SEVERE Expected exceptions/ errors (custom exceptions)- e.g. Trying to create a contact that already exist in the system - The system needs to throw "Contact_duplicated_exception" and show this to the user as "Contact was previously created. Could not proceed as name should be unique"
Model state errors (UI). Errors that I add to modelstate to showup on the page using @Html.ValidationSummary(true)

Client validation errors which are caught on the front end and application errors are caught in global.asax

I am looking for a good way to catch errors/exceptions of type 2 and 3 and return it back to AJAX() Calls with a certain severity type "Critical" vs "Warning".

I have been following the link for Handling validation errors on Ajax calls in Asp.net MVC

http://erraticdev.blogspot.com/2010/11/handling-validation-errors-on-ajax.html

I cannot generalize and add all the model state errors as ModelStateException. As most of these errors/exceptions are of a certain severity type and my front end (JQUERY) should read this type of error and do different actions.

1.How do I create a dictionary of custom exception , severity type ? 2.How do I throw these and add it to model state error. 3.How do I catch these globally using Custom Handle Erro Arrtibute and IExceptionFilter

Thanks,

推荐答案

抛出是异常,而不是错误。例外不是错误(惊讶?:-))。例外情况不能退回。



此外,没有好的或坏的方式来捕获异常,只有一个,try-catch-finally块。更重要的是在哪里抓住它们(许多初学者在没有好的目的的情况下抓得太多)以及如何处理它们。它通常需要理解异常机制和传播机制。



现在,你不能在服务器端抛出异常并在客户端捕获它。不仅因为网络分开了两边(有通过编组和重新抛弃异常的方法),但主要是因为.NET和JavaScript上的异常具有不同的不兼容性,即实现。



因此,您需要在服务器端,最顶层的堆栈帧上捕获ASP.NET应用程序的所有异常,并通过处理几个已知的基类型对其进行分类例外。不要忘记通过在try-catch块的最后处理非常根类型 System.Exception 来捕获所有其他异常(最后,如果你有最后的部分);这样的例外可以被归类为其他或类似的东西。



在您的HTTP请求中,创建一个可以作为纯文本布局并传递回的结构客户端。 Ajax将能够解析此文本并以您在页面上编程的方式显示异常信息。如果此类共享异常结构看起来相当复杂,请将此文本视为XML或JSON。这样,您就可以在服务器端生成异常结构并在JavaScript中解析它。当然,最方便的JavaScript形式是JSON。



-SA
Thrown are exceptions, not errors. Exceptions are not errors (surprise? :-)). Exceptions cannot be "returned".

Also, there are no good or bad ways of catching exceptions, there is only one, try-catch-finally block. More important thing is where to catch them (many beginners catch way too much without a good purpose) and what to do to handle them. It generally needs understanding of exception mechanism and the mechanism of propagation.

Now, you cannot throw an exception on the server side and catch it on the client side. Not only because the sides are separated by the network (there are the ways to thunk exceptions through marshaling and re-throwing on the other side), but mostly because exceptions on .NET and JavaScript have different incompatible nature, implementation.

Therefore, you need to catch all exceptions of your ASP.NET application on server side, on the very top stack frame, and classify it by handling several known base types of exception. Don't forget to catch all other exception by handling the very root type, System.Exception, at the very end of the try-catch block (before finally, if you have the finally part); such exceptions could be classified as "others" or something like that.

In your HTTP request, create a structure which can be laid out as plain text and passed back to the client. Ajax will be able to parse this text and show exception information the way you program it on the page. If such shared exception structure appears to be rather complex, consider this text to be XML or JSON. This way, you will be able to generate the exception structure on server side and parse it in JavaScript. Naturally, most convenient form for JavaScript is JSON.

—SA


这篇关于模型状态错误(或)特定类型的自定义异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆