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

前端之家收集整理的这篇文章主要介绍了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(并且不相关,所以它有点令人困惑).我认为包装器将解决您的问题.

原文链接:https://www.f2er.com/aspnet/251950.html

猜你在找的asp.Net相关文章