Actions
Action methods are the methods which have one to one mapping with user requests. These methods execute requests and create aa response. Each Controller can have default action method, however that needs to be declared in RegisteRoutes() method of RouteConfig file.
Action selectors are the attributes which can be applied to action methods. With the help of action selectors, routing engine selects the correct action method for handling specific request. Action selectors also decide the behaviour of method on the basis of name given in front of the action method.
There are three types of action selectors attributes,
- ActionName
- NonAction
- ActionVerbs
ActionName
ActionName attribute allows you to use different name than the actual name of method.
Below is the sample for the same.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace FirstAspNetMVC.Controllers {
- public class EmplyeeController: Controller {
- // GET: Home
- public string Index() {
- return "This is ASP.Net MVC ActionName";
- }
- [ActionName("GetDetail")]
- public string Detail() {
- return "This is detail at " + DateTime.Now.ToString("T");
- }
- }
- }
Now, in the above example, ActionName attribute is used as GetDetail; hence after running the application, you need to enter the following URL in the browser http://localhost:62833/Emplyee/ GetDetail
NonAction
NonAction is another built-in attribute which indicates that a method of a Controller is not an action method. It is used when you want a method not to be treated as an action method.
Below is the sample for the same.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace FirstAspNetMVC.Controllers {
- public class EmplyeeController: Controller {
- // GET: Home
- public string Index() {
- return "This is ASP.Net MVC ActionName";
- }
- [ActionName("GetDetail")]
- public string Detail() {
- return "This is detail at " + GetTimeAsString;
- }
- [NonAction]
- public string GetTimeAsString() {
- return "This is detail at " + DateTime.Now.ToString("T");
- }
- }
- }

Krishna SumanthPosted Jun 5, 2017, 9:15 PM
Is nonaction attribute used for private helper methods? What is the point of having a Public NonAction method? Seems contradictory to me.
Krishna SumanthPosted Jun 5, 2017, 9:13 PM
Can you explain HttpOptions and HttpPatch further with the help of an example?
Richa YadavPosted Jun 1, 2017, 7:03 AM
Nice Article. Thanks...