Action selectors in Mvc
Action Selectors
Action selector is the attribute that can be applied to the action methods.
It helps routing engine to select the correct action method to handle a
particular request. MVC 5 includes the following action selector attributes:
- ActionName
- NonAction
- ActionVerbs
1. ActionName
ActionName attribute allows us to specify a different action name than the
method name. Consider the following example.
Example: ActionName
public
class
BookController :
Controller
{
public
BookController()
{
}
[
ActionName(
"find")]
public
ActionResult GetById(
int id)
{
// get Book from the database
return View();
}
}
In the above example, we have applied ActioName("find")
attribute to GetById action method. So now, action name is "find"
instead of "GetById". This action method will be invoked on http://localhost/Boook/find/1
request instead of http://localhost/Book/getbyid/1
request.
class
BookController :
Controller
{
public
BookController()
{
}
[
ActionName(
"find")]
public
ActionResult GetById(
int id)
{
// get Book from the database
return View();
}
}
3. NonAction
NonAction selector attribute indicates that a public method of a Controller
is not an action method. Use NonAction attribute when you want public method in
a controller but do not want to treat it as an action method.
For example, the GetStudent() public method cannot be invoked in the same
way as action method in the following example.
Example: NonAction
public
class
BookController :
Controller
{
public
BookController()
{
}
[
NonAction]
public
Book GetBook(
int id)
{
return BookList.Where(s => s.BookId == id).FirstOrDefault();
}
}
class
BookController :
Controller
{
public
BookController()
{
}
[
NonAction]
public
Book GetBook(
int id)
{
return BookList.Where(s => s.BookId == id).FirstOrDefault();
}
}
3. ActionVerbs
You can apply these attributes to action method to indicate the kind of Http request the action method supports. If you do not apply any attribute then it considers it a GET request by default
[HttpGet], [HttpPost], [HttpPut], [HttpDelete] Attributes in ActionSelectors
[HttpGet] - It is used to restrict an action method to handle only HTTP GET requests.
[HttpPost] - It is used to restrict an action method to handle only HTTP POST requests.
[HttpPut] - It is used to restrict an action method to handle only HTTP PUT requests.
[HttpDelete] - It is used to restrict an action method to handle only HTTP DELETE requests.
Example: ActionVerbs
public
class
BookController :
Controller
{
public
BookController()
{
}
[
HttpGet]
public
Book GetBook(
int id)
{
return BookList.Where(s => s.BookId == id).FirstOrDefault();
}
}
class
BookController :
Controller
{
public
BookController()
{
}
[
HttpGet]
public
Book GetBook(
int id)
{
return BookList.Where(s => s.BookId == id).FirstOrDefault();
}
}
Points to Remember :
- MVC framework routing engine uses Action Selectors attributes to determine which action method to invoke.
- Three action selectors attributes are available in MVC 5:
ActionName, NonAction, ActionVerbs
- ActionName attribute is used to specify different name of action than method name.
- NonAction attribute marks the public method of controller class as non-action method. It cannot be invoked.
- MVC framework routing engine uses Action Selectors attributes to determine which action method to invoke.
- Three action selectors attributes are available in MVC 5:
- ActionName attribute is used to specify different name of action than method name.
- NonAction attribute marks the public method of controller class as non-action method. It cannot be invoked.
No comments: