MVC 4中的HttpContext.Current.Request.IsAjaxRequest()错误

des*_*guy 9 asp.net-mvc-4

我在用

HttpContext.Current.Request.IsAjaxRequest() 
Run Code Online (Sandbox Code Playgroud)

在Application_Error方法中检查global.asax中的ajax请求的条件,但是我收到以下错误:

'System.Web.HttpRequest'不包含'IsAjaxRequest'的定义和最佳扩展方法重载'System.Web.Mvc.AjaxRequestExtensions.IsAjaxRequest(System.Web.HttpRequestBase)'有一些无效的参数

以下是代码:

void Application_Error(object sender, EventArgs e)
    {

        Exception exception = Server.GetLastError().GetBaseException();
        HttpException httpException = exception as HttpException;

        string ErrorMessage = "";
        ErrorMessage = "Application Level Error";


        logger.Error(ErrorMessage, exception);

        if (System.Web.HttpContext.Current.Request.IsAjaxRequest()) //if its an ajax do not redirect
        {
            return;
        }
    else
    {
      Server.ClearError();
      this.Response.RedirectToRoute("Default", new { controller = "Home", action = "Error" });
    }
  }
Run Code Online (Sandbox Code Playgroud)

Pet*_*ete 28

猜猜它有用......发布作为答案.

尝试

new HttpRequestWrapper(System.Web.HttpContext.Current.Request).IsAjaxRequest() 
Run Code Online (Sandbox Code Playgroud)

IsAjaxRequest()取一个HttpRequestBase与a不同的HttpRequest(并且不相关,所以它有点令人困惑).我认为包装器将解决您的问题.