0% found this document useful (0 votes)
70 views

MVC - Action Selectors

Action selectors attributes in ASP.NET MVC help the routing engine select the correct action method to handle requests. The ActionName attribute allows specifying a different action name than the method name. The NonAction attribute indicates a public method is not an action method. ActionVerbs attributes like HttpGet and HttpPost determine which action method is selected based on the HTTP request method.

Uploaded by

Abhijit Dash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views

MVC - Action Selectors

Action selectors attributes in ASP.NET MVC help the routing engine select the correct action method to handle requests. The ActionName attribute allows specifying a different action name than the method name. The NonAction attribute indicates a public method is not an action method. ActionVerbs attributes like HttpGet and HttpPost determine which action method is selected based on the HTTP request method.

Uploaded by

Abhijit Dash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Action Selectors:

Action selectors are attributes that can be applied to the controller’s action methods to influence the
selection of an action method. It helps routing engine to select the correct action method to handle a
particular request. ASP.Net MVC includes the following action selector attributes:
1. ActionName
2. NonAction

3. ActionVerbs

ActionName:
ActionName attribute allows us to specify a different action name than the method name.
It plays a very crucial role when you are writing your action methods. This selector will decide the
behaviour of the method invocation based on the modified name given in front of the action method.
It is usually used to alias the name of the action method.
OR You can say:
This ActionName attribute is used when you expose an action name with a different name than its
method name. The ActionName selector attribute is used to change the name of action method.

Consider the following example.

Example: ActionName
public class StudentController : Controller
{
[ActionName("Find")]
public ActionResult GetStudentById(int id)
{
// get student from the database
return View();
}
}
In the above example, we have applied ActionName("Find") attribute to GetStudentById action
method. So now, action name is "Find" instead of "GetStudentById". This action method will be
invoked on http://localhost:54321/Student/Find/1 request instead
of http://localhost:54321/Student/GetStudentById/1 request.

NonAction:
NonAction selector attribute indicates that a public method of a Controller is not an action method.
Use NonAction attribute when we 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.
public class StudentController : Controller
{
[NonAction]
public Student GetStudent(int id)
{
return db.Students.Where(s => s.StudentId == id).FirstOrDefault();
}
}
ActionVerbs:
In this section, we will learn about the ActionVerbs selectors attribute.
The ActionVerbs selector is used when we want to control the selection of an action method based on
an Http request method. For example, we 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.
MVC framework supports different ActionVerbs, such as HttpGet, HttpPost, HttpPut, HttpDelete,
HttpOptions & HttpPatch. We can apply these attributes to action method to indicate the kind of Http
request the action method supports. If we do not apply any attribute then it considers it a GET request
by default.
The following picture illustrates the HttpGET and HttpPOST action verbs.

The following table lists the usage of http methods:

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.


The following example shows different action methods supports different ActionVerbs:

Example: ActionVerbs
public class StudentController : Controller
{
public ActionResult Index()
{
return View();
}

[HttpPost]
public ActionResult PostActionMethod()
{
return View("Index");
}

[HttpPut]
public ActionResult PutActionMethod()
{
return View("Index");
}

[HttpDelete]
public ActionResult DeleteActionMethod()
{
return View("Index");
}

[HttpHead]
public ActionResult HeadActionMethod()
{
return View("Index");
}

[HttpOptions]
public ActionResult OptionsActionMethod()
{
return View("Index");
}

[HttpPatch]
public ActionResult PatchActionMethod()
{
return View("Index");
}
}

We can also apply multiple http verbs using AcceptVerbs attribute.


Example: AcceptVerbs
[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
public ActionResult GetAndPostActionMethod()
{
return RedirectToAction("Index");
}
Points to Remember:
1. MVC framework routing engine uses Action Selectors attributes to
determine which action method to invoke.
2. Three action selectors attributes are available in MVC

- ActionName
- NonAction

- ActionVerbs

3. ActionName attribute is used to specify different name of action than

method name.
4. NonAction attribute marks the public method of controller class as non-
action method. It cannot be invoked.

5. ActionVerbs are another Action Selectors which selects an action

method based on request methods e.g. GET, POST, PUT, & DELETE etc.

6. Multiple action methods can have same name with different action

verbs. Method overloading rules are applicable.

7. Multiple action verbs can be applied to a single action method using


AcceptVerbs attribute.

You might also like