Detecting AJAX Requests in ASP.NET Core
June 03, 2017 by Anuraj
ASP.NET Core
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 :)
Copyright © 2024 Anuraj. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Powered by Jekyll. Hosted with ❤ by GitHub