【问题标题】:MVC Custom Error handling issueMVC 自定义错误处理问题
【发布时间】:2014-10-27 11:13:35
【问题描述】:

我目前正在开发一个 MVC 应用程序。我的问题是我的自定义错误处理无法正常工作。

所以基本上发生的事情是我抛出一个异常来在控制器中测试它。然后应用程序应该将用户重定向到 ~/Shared/Errors.cshtml 但由于某种原因它没有这样做。

这里是代码。

抛出异常:

throw new Exception("Something went wrong");

网页配置文件:

<customErrors mode="RemoteOnly"/>

错误控制器代码:

  public class ErrorController : Controller
    {
        //
        // GET: /Error/
    public ActionResult Index()
    {
        return View();
    }

}

Error.cshtml 中使用的代码:

@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>

提前感谢您提供的任何帮助

【问题讨论】:

标签: c# asp.net-mvc asp.net-mvc-4 error-handling


【解决方案1】:

你应该像这样返回视图。

return View("~/Views/Shared/Errors.cshtml");

【讨论】:

    猜你喜欢
    • 2011-06-23
    • 2010-09-30
    • 2010-11-13
    • 2010-12-24
    • 2010-09-27
    • 1970-01-01
    • 2016-04-10
    • 2014-12-01
    相关资源
    最近更新 更多