我有一个启用了 customErrors 的网站,它将我引导到一个自制的 View 。但是如果出现服务器错误,我想在 SorryPage View 上显示堆栈跟踪。为什么?因为这样用户就可以复制粘贴错误并将其发送到我们公司的 IT 帮助台,这样我们就知道出了什么问题以及错误来自何处。
我以前见过这个,但不知道该怎么做。非常感谢任何帮助!
Controller :
public ActionResult SorryPage()
{
return View("Error", new ErrorModel(ErrorMessages.SorryPage, "General", "Home", Labels.GoBackToPortal));
}
查看:
@using ILVO.Web.Resources
@model ILVO.Web.Areas.General.Error._Error.ErrorModel
@{
ViewBag.Title = "Error pagina";
Layout = "~/Views/Layouts/_Layout.cshtml";
}
<h1>@Html.Label("Error", Labels.TitleErrorPage)</h1>
<br/>
<h2>@Model.ErrorMessage</h2>
<br/>
<div class="margin-bottom5px">
@Html.ActionLink(Model.Button, "Index", Model.ToController, new { Area = Model.ToArea }, new { @class = "button" })
</div>
型号:
public class ErrorModel
{
public ErrorModel(string errorMessage, string toArea, string toController, string button)
{
ErrorMessage = errorMessage;
ToArea = toArea;
ToController = toController;
Button = button;
}
public string ErrorMessage { get; set; }
public string ToArea { get; set; }
public string ToController { get; set; }
public string Button { get; set; }
}
我的cfg文件:
<customErrors mode="RemoteOnly" xdt:Transform="Replace" defaultRedirect="~/General/Error/SorryPage">
<error statusCode="500" redirect="~/General/Error/SorryPage"/>
<error statusCode="404" redirect="~/General/Error/NotFound"/>
</customErrors>
最佳答案
最简单的方法是禁用自定义错误。您将获得经典的“死亡蓝屏”,其中包括异常消息和堆栈跟踪。它应该看起来很丑,这样你就不会向世界展示它。
在<system.web>
下的web.config里面部分添加以下内容:
<customErrors mode="Off" />
您还可以在 web.Debug.config 中进行设置,以便仅将其部署到您的测试环境中。您将需要设置发布或构建服务器,并且您的测试环境获得调试配置。请注意插入转换的使用。
<system.web>
<customErrors mode="Off" xdt:Transform="Insert" />
</system.web>
关于asp.net-mvc - 如何在错误 View 中显示堆栈跟踪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23561451/