Action Selectors In ASP.NET MVC

What are Action Selectors

Action Selector is basically an attribute, which you can apply on any action method in MVC Controller. It helps your routing engine to select the correct method to handle the particular request. In MVC 5, Action Selectors are very helpful and popular. MVC 5 includes 3 Action Selectors attributes

  1. Action Name.
  2. Non Action.
  3. Action Verbs.

Action Name

Action Name attribute allows and helps us to specify the different Action Names rather than method names. It means that you can hit URL in the Browser by your Action Name rather than the method name given above on your Action Result method. The example is given below.
ActionName Attribute

The example given above shows that we have applied ActionName(“findid) attribute to GetById Action method.

Thus, now Action name is “findid” instead of “GetById”. Thus, the Action Method will be invoked on

http://localhost/Employee/findid/1 request instead of http://localhost/Employee/getbyid/1 request.

Non Action

Non Action selector attribute helps us to indicate that your public method of a Controller is not an Action method. The main use of Non Action attribute is when you want public method in a Controller, it does not treat it as an Action method.

For example, the GetEmployee() public method cannot be invoked in the same way like your Action method is invoked. The code snippet is given below.
NonAction Attribute

Action Verbs

Action Verbs are very popular nowadays in APIs. This selector is basically used when you want to control the Action method based on Http request method. Here, you can define two different Action methods with the same name but one Action method acts or responds to HTTP Get request method and another Action method responds to HTTP Post request method.

The famous Action Verbs supported by MVC framework are HttpGet, HttpPost, HttpPut, HttpDelete, HttpOptions & HttpPatch. In MVC Framework, if you do not apply any attribute over the method, by default, it considers a GET request method.

Mainly, Http Method represents a request for the information about its communication options, which are supported by the Web Server.

I hope you have enjoyed this article. Stay tuned for the next article.


Similar Articles