Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge
  • All
  • C#
  • MVC
  • Web API
  • Azure
  • IIS
  • JavaScript
  • Angular
  • Node.js
  • Java
  • Python
  • SQL Server
  • SEO
  • Entrepreneur
  • Productivity

Define custom action selectors in ASP.NET MVC 5

Action selectors are attributes that can be applied to action methods. The MVC framework uses Action Selector attribute to invoke correct action.

You can create custom action selectors by implementing the ActionMethodSelectorAttribute abstract class.

For example, create custom action selector attribute AjaxRequest to indicate that the action method will only be invoked using the Ajax request as shown below.

Example: Create a Custom Action Selector
public class AjaxRequest: ActionMethodSelectorAttribute
{
    public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
    {
        return controllerContext.HttpContext.Request.IsAjaxRequest();
    }
}

In the above code, we have created the new AjaxRequest class deriving from ActionMethodSelectorAttribute and overridden the IsValidForRequest() method.

So now, we can apply AjaxRequest attributes to any action method which handles the Ajax request, as shown below:

Example: Apply Custom Action Selector
[AjaxRequest()]
[HttpPost]
public ActionResult Edit(int id)
{
    //write update code here..

    return View();
}

Thus, you can create custom action selector attributes for your requirements.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.