System.Web.HttpContextBase'不包含定义'目前'MVC 4 ELMAH记录 [英] System.Web.HttpContextBase' does not contain a definition for 'Current' MVC 4 with Elmah Logging

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

问题描述

我有一个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我有这个测试code我控制器的方法;

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'不包含一个定义
  当前和没有扩展方法当前接受第一个参数
  类型'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

这是因为控制器类定义了一个名为的HttpContext被定义属性作为

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

public HttpContextBase HttpContext { get; }

上的HttpContext Controller类收益HttpContextBase不具有一个Current属性。

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'不包含定义'目前'MVC 4 ELMAH记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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