我如何连接/替换“最终用户”?耶索德?
我想在开发期间或在服务器本地提供有用信息时显示 YSOD,但在其他情况下显示半通用页面。我知道我可以在 web.config 中设置应用程序的
配置标记的 defaultRedirect
属性,但我宁愿做一些处理来生成一个稍微更好的信息。
我的所有控制器都继承自一个中央 BaseController 类,我在其中重写了 OnException (基本上像这样):
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 以外的内容:这是我们的要求应用程序曾经有)。
I want to display the YSOD when it would be providing useful information during development or locally on the servers but a semi-generic page in other cases. I know that I could set the defaultRedirect
attribute of the application's <customErrors>
configuration tag in web.config, but I would rather do some processing to generate a page with slightly better information.
All of my controllers inherit from one central BaseController class where I have overridden OnException (essentially like this):
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));
}
How should I implement FigureOutIfDetailedYsodWouldBeDisplayed
(answer need not be code, a pointer in the right direction would be just fine)?
My current implementation checks the raw url for the existence of "//localhost", but this solution feels clumsy and doesn't work all the time (for example if the dev has a host entry to type something other than localhost: a requirement our app used to have).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您实际上使用开发环境的“调试”设置和生产环境的“发布”设置进行构建,那么您始终可以这样做
If you actually build using the Debug setting for Dev environments, and Release for production environments, you could always just do
您是否知道您还可以为每个 HTTP 错误代码添加自定义错误页面?那么您真的需要这个自定义错误处理代码吗?
另外,除非您确实有特定的需求,否则我建议您不要自己编写此异常日志记录代码。帮自己一个忙,看看 ELMAH。严重地。 [免责声明:我没有参与这个项目,只是一个非常快乐的用户]
它所需要的只是(几乎!)你的 web.config 中的几行,在你的 bin 文件夹中放置一个 dll,并且(如果你想登录到DB)单个 .sql 脚本。
来自项目页面:
“ELMAH(错误记录模块和处理程序)是一个应用程序范围的完全可插入的错误日志记录工具可以动态添加到正在运行的 ASP.NET Web 应用程序,甚至是计算机上的所有 ASP.NET Web 应用程序,无需重新编译或重新部署
ELMAH 。放入正在运行的 Web 应用程序并进行适当配置,您无需更改一行代码即可获得以下功能:
Did you know you can also add custom error pages per HTTP Error code? Do you really need this custom error handling code then?
Also, unless you have really specific needs, I would recommend not writing this exception logging code yourself. Do yourself a favor and check out ELMAH. Seriously. [disclaimer: I am not involved in this project, just a very happy user]
All it takes is (litterally!) a few lines in your web.config, dropping a dll in your bin folder, and (if you want to log to a DB) a single .sql script.
From the project page:
"ELMAH (Error Logging Modules and Handlers) is an application-wide error logging facility that is completely pluggable. It can be dynamically added to a running ASP.NET web application, or even all ASP.NET web applications on a machine, without any need for re-compilation or re-deployment.
Once ELMAH has been dropped into a running web application and configured appropriately, you get the following facilities without changing a single line of your code: