MVC – Action Verbs
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:
o
ActionName
o
NonAction
o
ActionVerbs
ActionName
·
ActionName attribute allows us to specify a
different action name than the method name.
public class EmployeeController : Controller
{
public
EmployeeController()
{
}
[ActionName("find")]
public ActionResult GetById(int id)
{
// get employee
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/employee/find/1 request instead of
http://localhost/employee/getbyid/1 request.
public class EmployeeController : Controller
{
public
EmployeeController()
{
}
[NonAction]
public Employee
GetEmployee(int id)
{
return
employeeList.Where(s => s.EmployeeId == id).FirstOrDefault();
}
}
|
·
This ActionName attribute is used when you
expose an action name with a different name than its method name, or you can
use an action name attribute to expose two methods with the same name as the
action with different names. The ActionName selector attribute is used to
change the name of action method.
[HttpGet], [HttpPost], [HttpPut], [HttpDelete]
Attributes in Action Selectors
·
[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.
ActionVerbs
·
The ActionVerbs selector is used when you
want to control the selection of an action method based on a Http request
method. For example, you can define two different action methods with the same
name but one action method responds to an HTTP Get request and another action
method responds to an HTTP Post request.
Http method
|
Usage
|
GET
|
To retrieve the information
from the server. Parameters will be appended in the query string.
|
POST
|
To create a new resource.
|
PUT
|
To update an existing resource.
|
HEAD
|
Identical to GET except that
server do not return message body.
|
OPTIONS
|
OPTIONS method represents a request
for information about the communication options supported by web server.
|
DELETE
|
To delete an existing resource.
|
PATCH
|
To full or partial update the
resource.
|
public class EmployeeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult PostAction()
{
return View("Index");
}
[HttpPut]
public ActionResult PutAction()
{
return View("Index");
}
[HttpDelete]
public ActionResult DeleteAction()
{
return View("Index");
}
[HttpHead]
public ActionResult HeadAction()
{
return View("Index");
}
[HttpOptions]
public ActionResult OptionsAction()
{
return View("Index");
}
[HttpPatch]
public ActionResult PatchAction()
{
return View("Index");
}
}
public class HomeController : Controller
{
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult GetEmployee()
{
var p = new EmployeeModel();
return View("Employee", p);
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Insert(EmployeeModel p)
{
return View("Employee");
}
[AcceptVerbs(HttpVerbs.Put)]
public ActionResult Update(EmployeeModel p)
{
return View("Employee");
}
[AcceptVerbs(HttpVerbs.Delete)]
public ActionResult Delete(int ID)
{
return View("Employee");
}
}
|
Comments
Post a Comment