91

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!

Razzie
31.3k12 gold badges67 silver badges82 bronze badges
asked Mar 26, 2015 at 15:15
6
  • 1
    As far as I can tell, it's still there. It's an extension method in System.Web.Mvc, class AjaxRequestExtensions. That's for MVC5, I don't know about MVC6... Commented Mar 26, 2015 at 15:21
  • 6
    That's what the OP is asking about: MVC6. Of course it's still there in MVC5. Commented Mar 26, 2015 at 15:37
  • 1
    @PatrykĆwiek Ok, yes, I'm testing MVC6. I might be wrong of course, but it seems to be missing from the "Microsoft.AspNet.Mvc": "6.0.0-beta3" package - or any of the other standard mvc-6 packages that comes with a new mvc-6 project. Commented Mar 26, 2015 at 15:38
  • Not sure about this but try just: IsAjaxRequest(), without the Request. prefix. Commented Mar 26, 2015 at 15:43
  • For a temporary solution on this, I decided to copy and modify the method from System.Web.Mvc.AjaxRequestExtensions. Hope it helps others, until this finds it way to the framework. public static class AjaxRequestExtensions { public static bool IsAjaxRequest(this HttpRequest request) { if (request == null) { throw new ArgumentNullException("request"); } return request.Headers != null && request.Headers["X-Requested-With"] == "XMLHttpRequest"; } } Commented Mar 26, 2015 at 15:57

5 Answers 5

133

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"
Javid
2,9653 gold badges40 silver badges63 bronze badges
answered Mar 26, 2015 at 15:56
Sign up to request clarification or add additional context in comments.

1 Comment

The source code from the version of MVC5 is here: github.com/ASP-NET-MVC/aspnetwebstack/blob/master/src/…
42

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

bool isAjaxCall = Context.Request.Headers["x-requested-with"]=="XMLHttpRequest";
radbyx
9,72022 gold badges93 silver badges133 bronze badges
answered Mar 15, 2017 at 3:43

2 Comments

what is namespace for Context ?
@Aligned No, there is nothing in the fetch spec about adding an identifier in the request headers. The X-Requested-With isn't in the XMLHttpRequest spec either ( hence the "X-" prefix), but all the vendors added it as a convention. There is no indication any convention will done for fetch. If your system needs to know then you need to manually add a header when creating your fetch request
11

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");
Saeed Zhiany
2,1419 gold badges34 silver badges44 bronze badges
answered Oct 23, 2019 at 6:33

Comments

4

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;
 }
answered Aug 19, 2020 at 21:12

Comments

1

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";
}
answered Sep 14, 2022 at 9:56

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.