Asp.Net Core MVC中的Request.IsAjaxRequest()在哪里? [英] Where is Request.IsAjaxRequest() in Asp.Net Core MVC?

查看:228
本文介绍了Asp.Net Core MVC中的Request.IsAjaxRequest()在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要了解有关令人兴奋的新Asp.Net-5框架的更多信息,我正在尝试使用新发布的Visual Studio 2015 CTP-6构建Web应用程序.

To learn more about the new exciting Asp.Net-5 framework, I'm trying to build a web application using the newly released Visual Studio 2015 CTP-6.

大多数事情看起来确实很有希望,但是我似乎找不到Request.IsAjaxRequest()-我在较老的MVC项目中经常使用的功能.

Most things looks really promising, but I can't seem to find Request.IsAjaxRequest() - a functionality I've been using quite frequently on older MVC projects.

是否有更好的方法可以使他们删除此方法-还是隐藏"在其他地方?

Is there a better way to do this - that made them remove this method - or is it "hidden" somewhere else?

感谢您在任何地方找到它的建议,或者该怎么做!

Thanks for any advice on where to find it or what to do instead!

推荐答案

我有点困惑,因为标题提到了MVC 5.

I got a little confused, because the title mentioned MVC 5.

在MVC6 github中搜索 Ajax repo没有给出任何相关结果,但是您可以自己添加扩展名.从MVC5项目中反编译给出了非常简单的代码段:

Search for Ajax in the MVC6 github repo doesn't give any relevant results, but you can add the extension yourself. Decompilation from MVC5 project gives pretty straightforward piece of code:

/// <summary>
/// Determines whether the specified HTTP request is an AJAX request.
/// </summary>
/// 
/// <returns>
/// true if the specified HTTP request is an AJAX request; otherwise, false.
/// </returns>
/// <param name="request">The HTTP request.</param><exception cref="T:System.ArgumentNullException">The <paramref name="request"/> parameter is null (Nothing in Visual Basic).</exception>
public static bool IsAjaxRequest(this HttpRequestBase request)
{
  if (request == null)
    throw new ArgumentNullException(nameof(request));
  if (request["X-Requested-With"] == "XMLHttpRequest")
    return true;
  if (request.Headers != null)
    return request.Headers["X-Requested-With"] == "XMLHttpRequest";
  return false;
}

因为 MVC6 Controller 似乎正在使用 Microsoft.AspNet. Http.HttpRequest ,则必须检查

Since MVC6 Controller seems to be using Microsoft.AspNet.Http.HttpRequest, you'd have to check request.Headers collection for appropriate header by introducing few adjustments to MVC5 version:

/// <summary>
/// Determines whether the specified HTTP request is an AJAX request.
/// </summary>
/// 
/// <returns>
/// true if the specified HTTP request is an AJAX request; otherwise, false.
/// </returns>
/// <param name="request">The HTTP request.</param><exception cref="T:System.ArgumentNullException">The <paramref name="request"/> parameter is null (Nothing in Visual Basic).</exception>
public static bool IsAjaxRequest(this HttpRequest request)
{
  if (request == null)
    throw new ArgumentNullException(nameof(request));

  if (request.Headers != null)
    return request.Headers["X-Requested-With"] == "XMLHttpRequest";
  return false;
}

或直接:

var isAjax = request.Headers["X-Requested-With"] == "XMLHttpRequest"

这篇关于Asp.Net Core MVC中的Request.IsAjaxRequest()在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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