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

查看:23
本文介绍了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.

搜索Ajax 在 MVC6 github 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,您必须检查 request.Headers 集合 通过对 MVC5 版本进行一些调整来获得适当的标头:

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天全站免登陆