在asp.net MVC 3错误处理 [英] Error Handling in asp.net mvc 3
问题描述
有一个内置或处理asp.net mvc的3个错误之有道?
Is there a built in or a proper way to handle errors in asp.net mvc 3?
这是我想做的事:
- 如果该应用程序崩溃或抛出一个错误,它关系到一个特定的错误页面。
- 我可以从控制器动作把我自己的错误。 (和它关系到一个错误页)。
我发现了以下几种方式:
I found the following ways:
- 我看到有一个很长的路要做到这一点
<一href=\"http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404s-in-asp-net-mvc/2577095#2577095\">here. (为v1和v2,而且
适用于V3)。 - 使用errorhandle属性这里。
- I see there is a long way to do it here. (for v1 and v2 but also applies to v3).
- Using errorhandle attribute here.
我如何处理这个正确的方法是什么?
How do I handle this the proper way?
如果该解决方案是类似或像上述列表#1,我使用ninject,我没有创建一个基类。我怎么还这样做呢?
If the solution is similar or is like #1 in the list above, I am using ninject and I have not created a base class. How do I still do this?
推荐答案
全局错误处理
所有你需要做的是改变的customErrors模式=开在web.config中页
通过Error.cshtml所在的共享文件夹中会显示错误。
确保Error.cshtml布局不为空。
[前人的精力它是这样的:@ {布局=〜/查看/共享/ _Layout.cshtml }
或删除布局= NULL code座]
对于Error.cshtml样本标记: -
For Global Error Handling
All you have to do is change the customErrors mode="On" in web.config page
Error will be displayed through Error.cshtml resides in shared folder.
Make sure that Error.cshtml Layout is not null.
[It sould be something like: @{ Layout = "~/Views/Shared/_Layout.cshtml"; }
Or remove Layout=null code block]
A sample markup for Error.cshtml:-
@{ Layout = "~/Views/Shared/_Layout.cshtml"; }
@model System.Web.Mvc.HandleErrorInfo
<!DOCTYPE html>
<html>
<head>
<title>Error</title>
</head>
<body>
<h2>
Sorry, an error occurred while processing your request.
</h2>
<p>Controller Name: @Model.ControllerName</p>
<p>Action Name : @Model.ActionName</p>
<p>Message: @Model.Exception.Message</p>
</body>
</html>
的具体错误处理
的HandleError属性添加到控制器类的具体行动。提供'查看'和'ExceptionType对于具体的错误。
样本NotImplemented异常处理程序:
For Specific Error Handling
Add HandleError attribute to specific action in controller class. Provide 'View' and 'ExceptionType' for that specific error.
A sample NotImplemented Exception Handler:
public class MyController: Controller
{
[HandleError(View = "NotImplErrorView", ExceptionType=typeof(NotImplementedException))]
public ActionResult Index()
{
throw new NotImplementedException("This method is not implemented.");
return View();
}
}
这篇关于在asp.net MVC 3错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!