我希望在开发期间或在服务器本地提供有用信息时显示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/