使用 JSON 和 jQuery 在 MVC3 中进行异常处理和状态消息
我正在用 MVC3 和 C# 编写代码。我想在发生异常和自定义异常时向用户发送用户友好的消息。我正在考虑使用 JSON 和 jQuery 弹出框。 你能告诉我该怎么做吗?有关于这个主题的教程或文章吗?
编辑:
我想创建扩展 IExceptionFilter 的自定义 ActonFilter。自定义过滤器捕获异常(如果抛出异常)并返回自定义类 ShowMessage(ex)。自定义类返回包含所需消息的 JSON 结果。在 jQuery 中,有一个解析器,它显示带有消息的弹出框(如果有例外)。
I am writing my code in MVC3 and C#. I want to send user friendly messages to the user when exceptions and custom exceptions occur. I am thinking of using JSON and jQuery popup boxes.
Can you tell me how to do this? Is there a tutorial or article on this topic?
EDIT:
I want to create custom ActonFilter that extends IExceptionFilter. The custom filter catches exceptions (if they are thrown) and returns custom class ShowMessage(ex). The custom class returns JSON result containing the desired message. In the jQuery there is a parser that shows the popup box with the message (if there is an exception).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我似乎正确理解你的问题,有很多方法可以做到这一点。
我的控制器看起来像这样:
然后我的视图看起来像这样:
这是一个简单的联系页面示例,具有 ajax 的完整功能,并通过带有动画 gif 的 css 背景的加载 div 警告用户何时发生某些情况并让他们知道他们的结果成功/失败。
您还可以通过使用 ActionResult、调用它并返回 Content
This 后半部分来实现类似的效果以及更多效果 - 只需将其视为伪代码,因为我只是即时编写它,但应该进行一些小的调整。
希望这对您有帮助,并且我不会因为做错事而被嘲笑,但是,如果我这样做并且有人可以向我展示更好的方法,我也很乐意改善自己:)
There are a number of ways to go about doing this if I seem to understand your question correctly.
My controller would look like this:
My view would then look something like:
This is a simple example of a contact page with full functionality for ajax and warning the user of when something is happening via a loading div with a css background of an animated gif and also letting them know their outcome success/fail.
You can also achieve a similar effect and more by using an ActionResult, calling that and returning Content
This later half - just consider it as pseudo code as I just wrote it up on the fly but should work with small tweaks.
Hopefully this is of help to you and I don't get laughed at for doing something incorrectly, however, if I did and someone can show me a better way I would love to better myself as well :)