2

Is there anybody let me know why IsAjaxRequest is deprecated in ASP.Net Core or is there any alternative function.

I used this function allot in my projects now I want to upgrade to ASP.Net Core

        if (Request.IsAjaxRequest())
        {
            return PartialView("index2", VMJob);
        }
        return View(VMModel);

there is a work around: Where is Request.IsAjaxRequest() in Asp.Net Core MVC?

but I want to know is there any builtin function in ASP.Net core

MJ X
  • 8,506
  • 12
  • 74
  • 99
  • 2
    There is no need for it anymore, because with the rewrite of ASP.NET to ASP.NET Core both WebAPI and MVC are now one and the same API. Also it's bad practice to mix MVC and WebAPI in one single action, because MVC uses Controller + Action + Http Verb whereas WebAPI uses controller + Http Verb only (action names are not used to identify WebAPI routes), i.e. `POST /Home/User/1/Edit` in MVC vs. `POST/PUT /api/Home/User/`and former is not a well formed REST uri/route. Also Remember, ASP.NET MVC was created at a time before WebAPI existed – Tseng Aug 08 '17 at 06:03
  • how to handle above ajax request in asp.net core – MJ X Aug 08 '17 at 06:30
  • Clean way: make two endpoints, one for Ajax one for mvc. Dirty way, use the linked article – Tseng Aug 08 '17 at 06:32
  • Do you have a sample code post it as answer so that I check it? that works for me or not – MJ X Aug 08 '17 at 06:35
  • @Tseng WebAPI wasn't the only reason this was needed. SPA based web apps need this more than ever. Just recreate the extension, using the original code for MVC5 – Robert Slaney Mar 27 '20 at 02:36

1 Answers1

0

There is a way to restrict your action method to respond only to ajax requests:

public class AjaxOnlyAttribute : ActionMethodSelectorAttribute
{
    public override bool IsValidForRequest(RouteContext routeContext, 
        ActionDescriptor action)
    {
        HttpRequest request = routeContext.HttpContext.Request;
        if (request == null)
        {
            return false;
        }

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

        return false;
    }
}

Then in your controller:

[Route("api/[controller]")]
public class UsersController : Controller
{
    [HttpGet]
    [AjaxOnly]
    public IActionResult GetAll()
    {
        return Ok(new[]
        {
            new UserDto{Id = 1, Name = "John"},
            new UserDto{Id = 2, Name = "Snow"}
        });
    }

    private class UserDto
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
}

This way you can create two separate action methods one for your ajax requests and one for standard full page reload.

regnauld
  • 4,046
  • 3
  • 23
  • 22
  • Well, the idea is that when you mark your action method with the AjaxOnly attribute and the request is not ajax, for example you type directly in the browser http://localhost/api/users, which is not ajax, the action method will not be resolved. Additionally it shows you a workaround for the lack of the `IsAjaxRequest` method and how to check if the method is ajax or not. – regnauld Aug 08 '17 at 10:58