简体   繁体   English

在Web上处理客户端和服务器端错误的正确方法是什么?

[英]What is the correct way to handle client side and server side errors on the web?

I'm new to web development (though not new to WinForms developmet) and I wanted to know the correct and efficient way to handle errors both on the client side as well as the server side. 我是Web开发的新手(虽然不是WinForms developmet的新手),我想知道在客户端和服务器端处理错误的正确有效方法。

For example, user enters a bad zip code (using letters), I want to notify the user of the bad zip code entered, how do I handle this? 例如,用户输入一个坏的邮政编码(使用字母),我想通知用户输入的坏邮政编码,我该如何处理?

I'm assuming this would be on the client side but can anyone show me an example code? 我假设这将在客户端,但任何人都可以给我一个示例代码? What scripting language should you use? 你应该使用什么脚本语言? Should you display a message on the screen? 你应该在屏幕上显示一条消息吗? Should you make an error message visible? 你应该看到错误信息吗? What is the standard way of doing this? 这样做的标准方法是什么?

Example two: user clicks on something that runs a query in a server database. 示例二:用户单击在服务器数据库中运行查询的内容。 An error occurs. 发生错误。 How do I notify the user of the error? 如何通知用户错误?

Usually in Win Forms I can just do this: 通常在Win Forms中我可以这样做:

try
{
     CreateDirectoriesAndSystemFiles();
}
catch (Exception ex)
{
     ErrorLogger.LogError(ex.Source, ex.TargetSite.ToString(), "Error creating directories!; " + ex.Message);
     MessageBox.Show(ex.Message, "Error creating directories!", MessageBoxButtons.OK, MessageBoxIcon.Error);
     return;
}

How can I mimic the above in a web environment? 我怎样才能在网络环境中模仿上述内容?

1 For client notification, you can use Validators 1对于客户端通知,您可以使用Validators

Link : http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.requiredfieldvalidator.aspx 链接: http//msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.requiredfieldvalidator.aspx

This link is about RequiredFieldValidator 此链接与RequiredFieldValidator有关

You have also CompareValidator, CustomValidator etc .... 你还有CompareValidator, CustomValidator等....

Link : http://msdn.microsoft.com/en-us/library/ms972961.aspx 链接: http//msdn.microsoft.com/en-us/library/ms972961.aspx

That's good solution. 这是一个很好的解决方案。

2 For treatment exception, you can use Global.asax and Applicatio_Error 2对于处理异常,您可以使用Global.asaxApplicatio_Error

Link : http://msdn.microsoft.com/en-us/library/24395wz3%28v=vs.100%29.aspx 链接: http//msdn.microsoft.com/en-us/library/24395wz3%28v=vs.100%29.aspx

Most people use jQuery validate as its very easy to use whilst being extremely powerful and flexible. 大多数人使用jQuery验证,因为它非常容易使用,同时非常强大和灵活。 Docs are here http://docs.jquery.com/Plugins/Validation . 文档在这里http://docs.jquery.com/Plugins/Validation Microsoft include them by default in MVC 3 & 4 and use it for model validation client side so if you're using MS stack and MVC thats another good reason to use it. Microsoft默认将它们包含在MVC 3和4中,并将其用于模型验证客户端,因此如果您使用MS堆栈和MVC这是另一个使用它的好理由。 Things get a bit different in Web Forms as you tend to use the built in validation controls which handle both client and server side out of the box references here http://msdn.microsoft.com/en-us/library/a0z2h4sw(v=vs.100).aspx - these are exnsible through the Custom Validator types that allow you to specify server and client side methods that you write yourself to handle bespoke functionality. Web Forms中的情况有所不同,因为您倾向于使用内置的验证控件来处理客户端和服务器端开箱即用的引用http://msdn.microsoft.com/en-us/library/a0z2h4sw(v = vs.100).aspx - 这些通过Custom Validator类型是可以解析的,它们允许您指定自己编写的服务器和客户端方法来处理定制功能。 Hope this helps! 希望这可以帮助!

asp.net can do the same try/catch handling for server-side errors. asp.net可以针对服务器端错误执行相同的try / catch处理。

as far as input validation goes, you should look into Asp.net Input Validation Controls 就输入验证而言,您应该查看Asp.net输入验证控件

For example #2 you can redirect to error page 例如#2,您可以重定向到错误页面

try
{
     CreateDirectoriesAndSystemFiles();
}
catch (Exception ex)
{
     Response.Redirect("Error.aspx?err=" + ex.GetType());
}

and on the Error.aspx page just parse err parameter and show it to user. 并在Error.aspx页面上解析err参数并将其显示给用户。

Good idea is to log this in db (if you use it). 好主意是在db中记录它(如果你使用它)。

Depends on if you are using webforms or MVC. 取决于您是否使用webforms或MVC。

In MVC, generally for server side error handling I create an ApplicationController class that inherits and put my error handling, logging and redirect logic there then have my controllers inherit this class. 在MVC中,通常用于服务器端错误处理我创建了一个ApplicationController类,它继承并将我的错误处理,日志记录和重定向逻辑放在那里然后我的控制器继承了这个类。

For webforms I would create an error control and include this in my master page and feed the server side errors back to this. 对于webforms我会创建一个错误控件并将其包含在我的母版页中,并将服务器端错误反馈给此。

In either case, for client side you can use jquery and javascript for field validations. 在任何一种情况下,对于客户端,您可以使用jquery和javascript进行字段验证。

you're asking a lot of questions, i'll try and answer a few: 你问了很多问题,我会尝试回答一些问题:

  1. concerning validation (the zipcode example)- firstly, and I cannot stress this enough- allways validate things on the server side client-side code, such as javascript, can be easily turned off / circumvented. 关于验证(zipcode示例) - 首先,我不能强调这一点 - 总是验证服务器端的客户端代码,例如javascript,可以很容易地关闭/规避。 Even if you check things on the client side (and you should)- always check them on the server side as well. 即使您在客户端检查事物(并且您应该) - 也要在服务器端检查它们。
    That said, what validation framework to use depends on what display framework you're using. 也就是说,要使用的验证框架取决于您正在使用的显示框架。 I've used jquery's validation in the past, and was pleased with it. 我过去曾使用过jquery的验证 ,并对此感到满意。

  2. What scripting language should you use? 你应该使用什么脚本语言? well, if there was one correct answer to that one, then there wouldn't be much point in having so many, right :) 好吧,如果有一个正确的答案,那么有这么多,没有多大意义,对:)
    Anyway, jQuery is very popular, but so is knockoutJS , and I hear angular is gaining in popularity. 无论如何, jQuery非常受欢迎,但是knockoutJS也是如此,我听到角度越来越受欢迎。 just see which one you like 看看你喜欢哪一个

  3. for unexpected errors on the server-side, there had been some good suggestions. 对于服务器端的意外错误,有一些很好的建议。 I like Aghilas Yakoub's. 我喜欢Aghilas Yakoub的。

Best of luck 祝你好运

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

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