asp.net-mvc-4 – MVC 4中的HttpContext.Current.Request.IsAjaxRequest()错误

我在用
HttpContext.Current.Request.IsAjaxRequest()

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

‘System.Web.HttpRequest’ does not contain a definition for
‘IsAjaxRequest’ and the best extension method overload
‘System.Web.Mvc.AjaxRequestExtensions.IsAjaxRequest(System.Web.HttpRequestBase)’
has some invalid arguments

以下是代码:

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

解决方法

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

尝试

new HttpRequestWrapper(System.Web.HttpContext.Current.Request).IsAjaxRequest()

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

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....