Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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!

like image 688
mikal Avatar asked Mar 26 '15 15:03

mikal


People also ask

What is request IsAjaxRequest ()?

The IsAjaxRequest() actually works by simply performing a check for the X-Requested-With header, as seen in the actual implementation of the function from MVC5. public static bool IsAjaxRequest(this HttpRequestBase request) { if (request == null) throw new ArgumentNullException("request");

How does ASP NET core process a request?

The ASP.NET Core web server will convert the response from the application logic into a raw HTTP response and sends it back to the reverse proxy. The reverse proxy will then send it back to the browser.


2 Answers

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" 
like image 81
Patryk Ćwiek Avatar answered Oct 02 '22 19:10

Patryk Ćwiek


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

bool isAjaxCall = Context.Request.Headers["x-requested-with"]=="XMLHttpRequest"; 
like image 38
guhyeon Avatar answered Oct 02 '22 18:10

guhyeon