Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

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

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

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

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));
}

The

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

How can I get around this problem ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

To get a reference to HttpContext.Current you need replace

HttpContext.Current

with

System.Web.HttpContext.Current

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

public HttpContextBase HttpContext { get; }

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

Hence you need to properly resolve the namespace here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...