Posted by Anuraj on Saturday, June 3, 2017 Reading time :1 minute
This post is about detecting Ajax Requests in ASP.NET Core. In earlier versions of ASP.NET MVC, developers could easily determine whether the request is made via AJAX or not with IsAjaxRequest() method which is part of Request method. In this post I am implementing the similar functionlity in ASP.NET Core.
In this implementation, I will be checking whether the Request object contains header with name
X-Requested-With. And here is the extension implementation, which detects whether the request header contains X-Requested-With.
And here is the AjaxOnly attribute implementation, which you need to add in the action methods.
If the selected action method decorated with AjaxOnly attribute and if users try to access without the header, the response will be Not found.
Happy Programming :)
What do you think? I would like to hear your thoughts, suggestions, and questions in the comments section below.