MVC自定义错误处理问题
问题描述:
我目前正在研究MVC应用程序。我的问题是,我无法让我的客户端错误处理正常工作。MVC自定义错误处理问题
所以基本上会发生什么是我抛出一个异常来测试这个控制器。然后应用程序应该将用户重定向到〜/ Shared/Errors.cshtml,但由于某种原因,它不会这样做。
这是代码。
抛出异常:
throw new Exception("Something went wrong");
Webconfig文件:
<customErrors mode="RemoteOnly"/>
ErrorController代码:在Error.cshtml使用
public class ErrorController : Controller
{
//
// GET: /Error/
public ActionResult Index()
{
return View();
}
}
代码:
@model System.Web.Mvc.HandleErrorInfo
@{
Layout = "_Layout.cshtml";
ViewBag.Title = "Error";
}
<div class="list-header clearfix">
<span>Error</span>
</div>
<div class="list-sfs-holder">
<div class="alert alert-error">
An unexpected error has occurred. Please contact the system administrator.
</div>
@if (Model != null && HttpContext.Current.IsDebuggingEnabled)
{
<div>
<p>
<b>Exception:</b> @Model.Exception.Message<br />
<b>Controller:</b> @Model.ControllerName<br />
<b>Action:</b> @Model.ActionName
</p>
<div style="overflow:scroll">
<pre>
@Model.Exception.StackTrace
</pre>
</div>
</div>
}
</div>
在此先感谢您可以给予的任何帮助
答
您应该返回这样的视图。
return View("~/Views/Shared/Errors.cshtml");
通过检查它是否工作? –
2014-10-27 11:19:23
请检查此[文章](http://www.prideparrot.com/blog/archive/2012/5/exception_handling_in_asp_net_mvc)。 – 2014-10-27 11:26:34
我最近已经回答了类似的问题,看看它是否有帮助 - http://*.com/questions/26357340/asp-mvc-handleerror-attribute-doesnt-work/26359455#26359455 – SBirthare 2014-10-27 12:23:25