有没有内置或适当的方式来处理错误asp.net mvc 3?
这是我想做的:
>如果应用程序崩溃或抛出错误,它会转到特定的错误页面。
>我可以从控制器操作中抛出我自己的错误。 (并转到错误页面)。
我发现了以下几种方式:
>我看到有很长的路要走
here.(对于v1和v2,但也
适用于v3)。
>使用errorhandle属性here。
我如何处理这个正确的方法?
如果解决方案类似或像上面的列表中的#1,我使用ninject和我没有创建一个基类。我如何仍然这样做?
解决方法
用于全局错误处理
所有你需要做的是在web.config页面中更改customErrors模式=“开”
错误将通过Error.cshtml显示在共享文件夹中。
确保Error.cshtml布局不为null。
[它应该是这样:@ {Layout =“〜/ Views / Shared / _Layout.cshtml”; }}
或删除Layout = null代码块]
所有你需要做的是在web.config页面中更改customErrors模式=“开”
错误将通过Error.cshtml显示在共享文件夹中。
确保Error.cshtml布局不为null。
[它应该是这样:@ {Layout =“〜/ Views / Shared / _Layout.cshtml”; }}
或删除Layout = null代码块]
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”。
示例未实现异常处理程序:
public class MyController: Controller { [HandleError(View = "NotImplErrorView",ExceptionType=typeof(NotImplementedException))] public ActionResult Index() { throw new NotImplementedException("This method is not implemented."); return View(); } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。