asp.net-mvc 如何在 ASP.NET MVC 应用程序中获得“真正的”HttpContext?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/153630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can you get the "real" HttpContext within an ASP.NET MVC application?
提问by SHODAN
Unfortunately, I need to do this. I'm using ELMAH for my error log. Before I route to my error.aspx view, I have to grab the default ELMAH error log so I can log the exception. You used to be able to use
不幸的是,我需要这样做。我正在使用 ELMAH 作为我的错误日志。在我路由到我的 error.aspx 视图之前,我必须获取默认的 ELMAH 错误日志,以便我可以记录异常。你曾经可以使用
Elmah.ErrorLog.Default
However, this is now marked as obsolete. The compiler directs me to use the method
但是,这现在被标记为过时。编译器指示我使用该方法
Elmah.ErrorLog.GetDefault(HttpContext context)
MVC's context is of type HttpContextBase, which enables us to mock it (YAY!). How can we deal with MVC-unaware libraries that require the old style HttpContext?
MVC 的上下文是 HttpContextBase 类型,它使我们能够模拟它(是的!)。我们如何处理需要旧式 HttpContext 的 MVC-unaware 库?
回答by SHODAN
回答by rodrigo caballero
this.HttpContext.ApplicationInstance.Context

