Asp.net mvc 如何在错误视图中显示堆栈跟踪?

Asp.net mvc 如何在错误视图中显示堆栈跟踪?,asp.net-mvc,error-handling,stack-trace,Asp.net Mvc,Error Handling,Stack Trace,我有一个启用了customErrors的网站,它将我导向一个自制的视图。但是如果发生服务器错误,我希望在SorryPage视图上显示堆栈跟踪。为什么?因为用户可以复制粘贴错误并将其发送到我们公司的it帮助台,这样我们就知道哪里出了问题以及错误来自何处 我以前见过这个,但现在想不出怎么做了。非常感谢您的帮助 控制器: public ActionResult SorryPage() { return View("Error", new ErrorModel(ErrorMess

我有一个启用了customErrors的网站,它将我导向一个自制的视图。但是如果发生服务器错误,我希望在SorryPage视图上显示堆栈跟踪。为什么?因为用户可以复制粘贴错误并将其发送到我们公司的it帮助台,这样我们就知道哪里出了问题以及错误来自何处

我以前见过这个,但现在想不出怎么做了。非常感谢您的帮助

控制器:

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; }
}
<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>
查看:

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; }
}
<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>
我的cfg文件:

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; }
}
<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>

“因为这样用户就可以复制粘贴错误并将其发送到我们公司的it帮助台,这样我们就知道哪里出了问题以及错误的来源”

这些信息可以被黑客利用。你为什么不自己记录信息或发电子邮件呢


您也可以使用我的(免费)服务,它为您处理一切:

您只需为您的错误创建数据库,并使用过滤器将它们添加到数据库中即可

型号: 与数据库的连接: 视图: 您可以通过右键单击“索引”并选择template=List来创建视图,
model=ExceptionDetail,所有代码都将自动生成。

最简单的方法是禁用自定义错误。您将看到经典的“死亡黄屏”,其中包括异常消息和堆栈跟踪。它应该看起来很丑,这样你就不会把它展示给全世界

在web.config的
部分下添加以下内容:

<customErrors mode="Off" />

您还可以在web.Debug.config中进行设置,以便只将其部署到测试环境中。您将需要设置发布服务器或生成服务器,并且您的测试环境将获得调试配置。请注意插入变换的使用

<system.web>
  <customErrors mode="Off" xdt:Transform="Insert" />
</system.web>


是,但这是一个内部网站,因此只有登录到该域的员工才能看到该网站。我认为在这种情况下,风险很低。你显然不必处理PCI合规性问题。尽管如此,仅仅因为它是一个内部网站,就不能成为放松安全的借口。在这一特定场景中,这可能不是什么大不了的事情,但这是一个滑坡。如果你让自己在安全问题上偷懒,你会把事情搞砸的。