Where Is Request.Isajaxrequest() in ASP.NET Core MVC

Where is Request.IsAjaxRequest() in Asp.Net Core MVC?

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"

Request.IsAjaxRequest() alternative in MVC6

Check the user agent, as this:

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

Custom IsAjaxRequest function doesn't work in MVC 6

The issue was in the selector, since it was wrong e.preventDefault doesn't work and the request continues normally, that's why it will never be ajax request.

how to know if the request is ajax in asp.net mvc?

All AJAX calls made by jQuery will have a header added to indicate it is AJAX. The header to check is X-Requested-With, and the value will be XMLHttpRequest when it is an AJAX call.

Note that AJAX requests are normal GETs or POSTs, so unless you (or your AJAX library like jQuery) are adding an additional header in the request, there is no way to know for certain whether it is AJAX or not.

C# MVC detect if XMLHttpRequest

As Byron Jones pointed out in the answer I needed to set the the MLHttpRequest.setRequestHeader() my self and if you follow the link you will see it's as simple as adding

oReq.setRequestHeader('X-Requested-With', 'XMLHttpRequest');

so updated my code from the question like so:

// C# needs the line:
public IActionResult Create()
{
ViewData["xmlHttpRequest"] = false;
if (HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest") {
ViewData["xmlHttpRequest"] = true;
}

return View();
}

// and javascript like so:
function reqListener () {
console.log(this.responseText);
}

var oReq = new XMLHttpRequest();
oReq.addEventListener("load", reqListener);
oReq.open("GET", "https://localhost:5001/Movies/Create");
oReq.setRequestHeader('X-Requested-With', 'XMLHttpRequest'); // this is the only new line
oReq.send();

From the documentation it's stated

The XMLHttpRequest method setRequestHeader() sets the value of an HTTP
request header. When using setRequestHeader(), you must call it after
calling open(), but before calling send()
. If this method is called
several times with the same header, the values are merged into one
single request header.

Follow the links for more information.

Checking Request.IsAjaxRequest always return false inside my asp.net mvc4

It seems to me that I did not include the jquery.unobtrusive-ajax.min.js

How to check if request is ajax or not in codebehind - ASP.NET Webforms

You could create your own extension method much like the one in the MVC code

E.g.

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

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

HTHs,

Charles

Edit: Actually Callback requests are also ajax requests,

    public static bool IsAjaxRequest(this HttpRequest request)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
var context = HttpContext.Current;
var isCallbackRequest = false;// callback requests are ajax requests
if (context != null && context.CurrentHandler != null && context.CurrentHandler is System.Web.UI.Page)
{
isCallbackRequest = ((System.Web.UI.Page)context.CurrentHandler).IsCallback;
}
return isCallbackRequest || (request["X-Requested-With"] == "XMLHttpRequest") || (request.Headers["X-Requested-With"] == "XMLHttpRequest");
}

XMLHttpRequest() not recognized as a IsAjaxRequest?

Here's the code for IsAjaxRequest() in ASP.NET MVC 5

public static bool IsAjaxRequest(this HttpRequestBase request)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
return request["X-Requested-With"] == "XMLHttpRequest" || (request.Headers != null && request.Headers["X-Requested-With"] == "XMLHttpRequest");
}

It looks like there is a dependency on a certain header value (X-Requested-With) being in the request in order for that function to return true.

Here is some more info on X-Requested-With

What's the point of the X-Requested-With header?

You could always look at the jQuery $.ajax() code itself to see how that is setting the header. To be honest, I wouldn't bother doing ajax without jQuery anyway, it deals with all of these things for you.



Related Topics



Leave a reply



Submit