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.
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:
[AjaxRequest()]
[HttpPost]
public ActionResult Edit(int id)
{
//write update code here..
return View();
}
Thus, you can create custom action selector attributes for your requirements.
- View in ASP.NET MVC
- How to bind a model to a partial view in ASP.NET MVC?
- How to display an error message using ValidationSummary in ASP.NET MVC?
- How to enable client side validation in ASP.NET MVC?
- How to enable bundling and minification in ASP.NET MVC?
- How to pre-compile razor view in ASP.NET MVC?
- How to set image path in StyleBundle?
- What is RouteData in ASP.NET MVC?
- Difference between Html.RenderBody() and Html.RenderSection() in ASP.NET MVC
- Difference between Html.Partial() and Html.RenderPartial() in ASP.NET MVC
- How to use web.config customErrors in ASP.NET MVC?
- How to display a custom error page with error code using httpErrors in ASP.NET MVC?
- How to create a custom filter in ASP.NET MVC?
- Build e-commerce application on .NET Framework
- Redirect non-www to www domain in ASP.NET
- Redirect from HTTP to HTTPS in ASP.NET
- How to upload files in ASP.NET MVC?