System.Web.HttpContextBase' 不包含带有 Elmah 日志记录的“当前"MVC 4 的定义 [英] System.Web.HttpContextBase' does not contain a definition for 'Current' MVC 4 with Elmah Logging

查看:17
本文介绍了System.Web.HttpContextBase' 不包含带有 Elmah 日志记录的“当前"MVC 4 的定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

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

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

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

HttpContext.Current

HttpContext.Current

导致以下错误;

'System.Web.HttpContextBase' 不包含定义'Current' 和没有扩展方法 'Current' 接受第一个参数可以找到System.Web.HttpContextBase"类型的(您是否缺少使用指令还是程序集引用?)

'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?)

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

How can I get around this problem ?

推荐答案

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

To get a reference to HttpContext.Current you need replace

HttpContext.Current

System.Web.HttpContext.Current

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

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

public HttpContextBase HttpContext { get; }

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

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

因此您需要在这里正确解析命名空间.

Hence you need to properly resolve the namespace here.

这篇关于System.Web.HttpContextBase' 不包含带有 Elmah 日志记录的“当前"MVC 4 的定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆