C# System.Web.HttpContextBase' 不包含带有 Elmah 日志记录的“当前”MVC 4 的定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19431820/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:58:27  来源:igfitidea点击:

System.Web.HttpContextBase' does not contain a definition for 'Current' MVC 4 with Elmah Logging

c#asp.net-mvcasp.net-mvc-4

提问by neildt

I have a C# ASP.NET MVC 4 project, which is using Elmah for catching any unhandled exceptions. This works great in most situations.

我有一个 C# ASP.NET MVC 4 项目,它使用 Elmah 来捕获任何未处理的异常。这在大多数情况下都很有效。

However I've found that for a controller method that is called using a JQuery Ajax call, I can't get the current Context.

但是我发现对于使用 JQuery Ajax 调用调用的控制器方法,我无法获取当前上下文。

For example in my controller method that returns the JsonResult I have this test code;

例如,在我返回 JsonResult 的控制器方法中,我有这个测试代码;

try
{
    throw new Exception("This is a test");
}
catch(Exception e)
{
    Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(e));
}

The

HttpContext.Current

HttpContext.Current

is causing the following error;

导致以下错误;

'System.Web.HttpContextBase' does not contain a definition for 'Current' and no extension method 'Current' accepting a first argument of type 'System.Web.HttpContextBase' could be found (are you missing a using directive or an assembly reference?)

“System.Web.HttpContextBase”不包含“Current”的定义,并且找不到接受“System.Web.HttpContextBase”类型的第一个参数的扩展方法“Current”(您是否缺少 using 指令或程序集引用?)

How can I get around this problem ?

我怎样才能解决这个问题?

采纳答案by Dinesh

To get a reference to HttpContext.Current you need replace

要获得对 HttpContext.Current 的引用,您需要替换

HttpContext.Current

with

System.Web.HttpContext.Current

This is because Controller class defines a property named HttpContext that is defined as

这是因为 Controller 类定义了一个名为 HttpContext 的属性,该属性定义为

public HttpContextBase HttpContext { get; }

HttpContext on Controller class returns HttpContextBase which does not have a Current property.

Controller 类上的 HttpContext 返回没有 Current 属性的 HttpContextBase。

Hence you need to properly resolve the namespace here.

因此,您需要在此处正确解析命名空间。