c# - 我将如何连接/替换 “end user” YSOD?

标签 c# asp.net-mvc error-handling yellow-screen-of-death

我希望在开发期间或在服务器本地提供有用信息时显示YSOD,而在其他情况下提供半通用页面。我知道我可以在web.config中设置应用程序的defaultRedirect配置标签的<customErrors>属性,但是我宁愿进行一些处理以生成信息稍微好一点的页面。

我所有的 Controller 都继承自一个我重写了OnException的中央BaseController类(本质上是这样的):

protected override void OnException(ExceptionContext filterContext) {
    //if something really bad happened and we get inside this if, 
    //just let the YSOD appear because there isn't anything we can do
    if (filterContext == null)
        return; 

    LogException(filterContext.Exception);

    //insert answer for question here:
    if (FigureOutIfDetailedYsodWouldBeDisplayed(filterContext)) 
        return;

    //what to actually do for end users
    filterContext.ExceptionHandled = true;
    filterContext.Result = View("ErrorPage", GetErrorModel(filterContext));
}

我应该如何实现FigureOutIfDetailedYsodWouldBeDisplayed(答案不必是代码,正确方向的指针就可以了)?
我当前的实现会检查原始url是否存在“//localhost”,但是这种解决方案感觉很笨拙,并且一直都无法正常工作(例如,如果开发人员有一个主机项来键入除localhost外的其他内容:应用曾经拥有)。

最佳答案

如果实际上是针对Dev环境使用Debug设置进行构建,而针对生产环境使用Release进行构建,则始终可以

#if DEBUG
    //Show YSOD
#else
    //Show friendly error page
#endif

关于c# - 我将如何连接/替换 “end user” YSOD?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2224480/

相关文章:

c# - Asp.Net Mvc4 : How to use @ character in js file

c++ - 是否可以用调试中断或错误抛出替换所有标准错误对话框?

error-handling - 字符串 io::Errors 是如何创建的

javascript - 自定义验证适用于服务器端,但不适用于客户端 asp mvc 5

asp.net-mvc - 菜单上没有添加 Controller ,也无法在 MVC 4.5 中添加 View

c - C 中的短路错误处理

c# - 如何在 Serilog Sink 中使用依赖注入(inject)?

c# - Visual Studio 2015 中的共享项目和类库有什么区别?

C# winform 检查控件是否物理可见

C# 样式首选区域之间、方法之间和属性之间的间距