What is an Action in Controller ?
- Any public method on a Controller consider as an Action. Parameters on actions are bound to request data and validated using model binding.
public ActionResult ActionA()
{
// some codes
}
How can I restrict a public method in a controller to NOT invoked as Action Method?
- Using [NonAction] attribute, you can create a public method in controller class which is NOT accessed as an Action.
[NonAction]
public void MethodB()
{
// some codes
}
How can I change the Action method name?
[ActionName(“ActionX”)]
public ActionResult ActionA()
{
// some codes
}
How can you restrict an action method to be invoked only by HTTP GET, POST, PUT or DELETE?
By default, each and every action method in the controller can be invoked by any HTTP request (i.e. GET, PUT, POST, and DELETE).
[HttpGet] // other way [AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index()
{
//TODO:
return View();
}
Can you define multiple Http Verbs on a single action method?
Yes, Multiple HTTP verbs can be applied to a single action method using AcceptVerbs attribute
[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
public ActionResult Index()
{ //TODO:
return View();
}
So in the above example action method can be invoked by HttpGet request or HttpPost request.
POINTS TO REMEMBER
- public method should not be static.
- public method should not be overloaded.
- Any public method on a Controller consider as an Action. Parameters on actions are bound to request data and validated using model binding.
public ActionResult ActionA()
{
// some codes
}
How can I restrict a public method in a controller to NOT invoked as Action Method?
- Using [NonAction] attribute, you can create a public method in controller class which is NOT accessed as an Action.
[NonAction]
public void MethodB()
{
// some codes
}
How can I change the Action method name?
[ActionName(“ActionX”)]
public ActionResult ActionA()
{
// some codes
}
How can you restrict an action method to be invoked only by HTTP GET, POST, PUT or DELETE?
By default, each and every action method in the controller can be invoked by any HTTP request (i.e. GET, PUT, POST, and DELETE).
[HttpGet] // other way [AcceptVerbs(HttpVerbs.Get)]
public ActionResult Index()
{
//TODO:
return View();
}
Can you define multiple Http Verbs on a single action method?
Yes, Multiple HTTP verbs can be applied to a single action method using AcceptVerbs attribute
[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
public ActionResult Index()
{ //TODO:
return View();
}
So in the above example action method can be invoked by HttpGet request or HttpPost request.
POINTS TO REMEMBER
- public method should not be static.
- public method should not be overloaded.