在 Asp.Net 核心 MVC 中 Request.IsAjaxRequest()在哪里?

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

大多数东西看起来都很有前途,但是我似乎找不到 Request.IsAjaxRequest ()-一个我在旧的 MVC 项目中经常使用的功能。

有没有更好的方法来做到这一点-使他们删除这一方法-或者它“隐藏”在其他地方?

感谢任何建议,在哪里可以找到它或做什么代替!

45509 次浏览

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

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

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

or directly:

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

in asp.net core, You can use Context.Request.Headers.

bool isAjaxCall = Context.Request.Headers["x-requested-with"]=="XMLHttpRequest";

For those who are working on ASP.Net Core

HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest";

Example
Controller.cs

bool isAjax = HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest";


if (isAjax)
return Json(new { redirectTo = Url.Action("Index", "ControllerAction") });
else
return RedirectToAction("Index", "ControllerAction");

After using the solution provided above by Patryk Ćwiek above I noticed a potential issue (largely due to my incorrectly typing "XMLHttpRequest" as "XmlHttpRequest"), which resulted in an incorrect return value. To accommodate my mistake, I updated it slightly. Here's my updated version:

    public static bool IsAjaxRequest(this HttpRequest request)
{
if (request == null)
throw new ArgumentNullException(nameof(request));


if (request.Headers != null)
return !string.IsNullOrEmpty(request.Headers["X-Requested-With"]) &&
string.Equals(
request.Headers["X-Requested-With"],
"XmlHttpRequest",
StringComparison.OrdinalIgnoreCase);


return false;
}

Combining the top 2 answers, this is what worked for me

_ViewStart.cshtml (Entire Contents):

@{
bool isAjaxCall = Context.Request.Headers["x-requested-with"] == "XMLHttpRequest";
    

Layout = isAjaxCall ? null : "_YourLayoutName";
}