且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

你如何通过Asp.net MVC处理异常?

更新时间:2023-02-15 23:16:43

使用try \catch块



https://msdn.microsoft.com/en-GB/library/0yd65esw.aspx [ ^ ]


ASP.NET只是构建应用程序的框架,MVC可以作为框架的一种风味。如果你真的打开一个在ASP.NET上运行的项目,你会发现它是用C#(或VB.NET;都是.NET语言)编写的。因此,您处理的错误将在编程语言本身中处理。



C#处理这样的问题,



 尝试 {
// 此处代码
} catch (例外e){
// 在这里处理错误。
}





现在,这是处理一个白痴程序员( IMO )的例外情况。一位专业的ASP.NET程序员会尝试以这样的方式编写程序,不会引发异常。最明显的异常是System.NullReferenceException。他会尝试检查值是否为null,如下所示,



  if (obj!=  null ){
// 这里的代码
}





这也会以更好的方式处理异常。因为它可以让他以一种用户友好的方式停止应用程序。



另一种方法是使用自定义错误页面处理错误。如果应用程序中存在错误,您可以将用户发送到另一个页面。这在Web.config文件中是正确的,代码是这样的,



  <  配置 >  
< appsettings / >
< connectionstrings / >
< system.web >
< 编译 debug =an> true / >

<! - 启用自定义错误 - >
< customerrors mode = >
defaultRedirect =ErrorPage.aspx>
< 错误 statuscode
= 404 重定向 = Error404.aspx / >
< / customersrors >

< / system.web >
< / configuration >





这会处理错误并将重定向,但你将失去为什么异常提出。因此,我建议您不要使用此方法,而是以编程方式处理异常,并查看可以采取哪些措施来解决问题。



参考文献:



https://msdn.microsoft.com/en-us/library/bb397417.aspx

http://www.codeproject.com/Articles/10593/Error-Handling-in-ASP-NET


How do you handle the exception through Asp.net MVC?

Use a try\catch block

https://msdn.microsoft.com/en-GB/library/0yd65esw.aspx[^]


ASP.NET is just the framework for building applications, MVC can be taken as a flavor for the framework. If you actually, open a project that runs on ASP.NET, you will see that it is written in C# (or VB.NET; both being .NET languages). So the errors that you handle, are handled in the programming language itself.

C# handles the problems like this,

try {
   // Code here
} catch (Exception e) {
   // Handle the error here.
}



Now, that was the method to handle the exception by an idiot programmer (IMO). An expert ASP.NET programmer would try to write the program in such as way, that exceptions are not raised. Most obvious exception is, System.NullReferenceException. He would try to check if the value is null, like this,

if(obj != null) {
   // Code here
}



This would also handle the exception, in a much better way. Because it would allow him to stop the application in a much user-friendly way.

Another way is to handle the errors using custom error page. You can send the user to another page if there is an error in your application. That goes right in the Web.config file and the code is like this,

<configuration>
  <appsettings />
  <connectionstrings />
  <system.web>
    <compilation debug="true" />

    <!-- Turn on Custom Errors -->
    <customerrors mode="On">
      defaultRedirect="ErrorPage.aspx">
      <error statuscode="404" redirect="Error404.aspx" />
    </customerrors>

  </system.web>
</configuration>



This would handle the error and will redirect, but you will lose why was the exception raised. So I would recommend that you do not use this method, instead handle the exception programmatically and see what could have been done to fix the problem.

References:

https://msdn.microsoft.com/en-us/library/bb397417.aspx
http://www.codeproject.com/Articles/10593/Error-Handling-in-ASP-NET