ActionVerbs are used when we want to control the selection of an action method based on a Http request method.ย For example, if we have two action method with same name then it can be respond based upon the HTTP request type(one action has ย to respond to Http Get and another method have respond toย Http Post). ASP.NET MVC provides attributes to tweak how the actions can be selected based on the name.
Different types of ActionVerbs
ASP.NET MVC supports different types of ActionVerbs.
- HttpVerbs.Get – retrieve the details from the server. Parameters will be appended in the query string.
- HttpVerbs.Post –ย submitย new details.
- HttpVerbs.Put – Use to update an existing information.
- HttpVerbs.Delete – To delete an existing information.
- HttpVerbs.Head –ย Identical to GET except that server do not return message body
- HttpVerbs.Patch –ย Requests that a set of changes described in the request entity be applied to the resource identified by the Request
- HttpVerbs.Options –ย Represents a request for information about the communication options available on the request/response identified by the Request
In ASP .NET MVC, it can be achieve in two different ways.
Method 1: AcceptVerbs Attribute
We can apply multiple http verbs using AcceptVerbs attribute by using OR operator.
public class dotnethelpersController : Controller
{
[AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult EditEmpDetails(int id)
{
// retrieve the Details
}
Method 2: shortcut attributes
public class dotnethelpersController : Controller
{
[HttpGet]
public ActionResult EditEmpDetails(int id)
{
}
[HttpPost]
public ActionResult EditEmpDetails(string empCode)
{
}
Points to Remember :
- If we declare the ActionVerbs then the action methods are selected based on the request methods.
- Using AcceptVerbs attribute, we can use multiple action verbs in a single action method
- Multiple action methods can have same name by declaring with different action verbs.
Nice article. Very useful information