This article is a mirror article of machine translation, please click here to jump to the original article.

View: 19765|Reply: 1

[Source] ASP.NET Core (XIII) to determine whether it is an Ajax request or not

[Copy link]
Posted on 2022-3-31 19:30:16 | | | |
In previous ASP.NET MVC, you could use the Request extension method IsAjaxRequest() to determine if a request is an Ajax request. In ASP.NET Core, how can you tell if the request is an Ajax request or a direct URL accessed through the browser?

Determine if the request is an Ajax principle, judgeWhether the request header contains: X-Requested-With and the value is: XMLHttpRequest, as shown in the figure below:



So, the implementation code is as follows:

If we want to restrict the controller to allow certain methods to only be requested through AJAX, we can inheritActionMethodSelectorAttributefeature to implement the IsValidForRequest abstraction method, the code is as follows:

Then, add this feature to the controller method ifNon-Ajax requests this method will directly report a 404 Not found error

review

ASP.NET Core (twelve) front-end JS, CSS bundling, and compression
https://www.itsvse.com/thread-10282-1-1.html

ASP.NET Core (XI) endpoint route adds middleware to display all DI services
https://www.itsvse.com/thread-10269-1-1.html

ASP.NET Detailed explanation of Configuration priorities in Core(10).
https://www.itsvse.com/thread-10265-1-1.html

ASP.NET Detailed explanation of the Middleware middleware of Core (9).
https://www.itsvse.com/thread-9647-1-1.html

ASP.NET Detailed explanation of Core Middleware
https://www.itsvse.com/thread-8126-1-1.html

ASP.NET pit of the default parameters of the Swagger UI in Core(8).
https://www.itsvse.com/thread-9640-1-1.html

ASP.NET Core (7) In-depth analysis of the framework source code
https://www.itsvse.com/thread-9601-1-1.html

ASP.NET Core (VI) DI manually obtains the method of injecting objects
https://www.itsvse.com/thread-9595-1-1.html

ASP.NET Core (five) is based on CAP distributed transactions
https://www.itsvse.com/thread-9593-1-1.html

ASP.NET Core(4) filter unified ModelState model validation
https://www.itsvse.com/thread-9589-1-1.html

ASP.NET Core (iii) Dynamically create instances using ActivatorUtilities
https://www.itsvse.com/thread-9488-1-1.html

ASP.NET Core (2) Restart the application by code
https://www.itsvse.com/thread-9480-1-1.html

ASP.NET Core (1) uses Redis caching
https://www.itsvse.com/thread-9393-1-1.html
(End)





Previous:[Actual combat]. NET 6 Random generates a random number test
Next:Yandex search engine robots.txt Clean-param configuration
Posted on 2022-4-2 09:16:44 |
Learn to learn
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com