gpt4 book ai didi

c# - MVC自定义错误处理问题

转载 作者:行者123 更新时间:2023-12-03 08:02:40 25 4
gpt4 key购买 nike

我目前正在开发MVC应用程序。我的问题是我无法使我的customerror处理正常工作。

因此,本质上发生的是我抛出异常以在 Controller 中对此进行测试。然后,应用程序应将用户重定向到〜/Shared/Errors.cshtml,但由于某种原因,它不会这样做。

这是代码。

抛出异常:

throw new Exception("Something went wrong");

Webconfig文件:
<customErrors mode="RemoteOnly"/>

ErrorController代码:
  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>

预先感谢您可以提供的任何帮助

最佳答案

您应该像这样返回 View 。

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

关于c# - MVC自定义错误处理问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26586322/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com