Action Selectors & Action Verbs in ASP.NET Core

In this article, we look at what is Action selector and its role in selecting the controller action methods. In the last few tutorials on Routing in ASP.NET Core, We learnt how routing engine picks up the correct action method to execute when the Request arrives. The Action selectors i. e. Action Name, Non-Action and Action Verbs gives us more control in the URL Matching Process. 

What is Action Selector

Action selector is the attribute that can be applied to the controller action methods. These attributes help the Routing Engine to select the correct action method to handle the given URL.

The ASP.NET Core includes three types of the Action Selectors.

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

Action Name

The ActionName attributes define the name of the action. The routing engine will use this name instead of the method name to match the action name routing segment. You will use this attribute when you want to alias the name of the Action method.

In the code snippet above, Action Name for the Edit action is changed to Modify using the ActionName attribute. We can no longer invoke this method using the name Edit. We have to use the Action Name modify in the URL to invoke this action.

Remember that you can also use the route attribute to change the Action Name.

Non Action

The public methods in the controller class are called as Action methods. These methods can be invoked by Anyone located anywhere in the world by simply typing the URL in the browser.

You can let Routing Engine know that a particular method is not an Action method by decorating it with NonAction attribute as shown below

Action Verbs

The Action verbs selector is used when you want to control the Action method based on HTTP request method. This is done using the set of attributes provided by MVC, such as the HttpGet and HttpPost attributes, which collectively called as HTTP Attributes.  

There are several HTTP verbs available to be used in the ASP.NET core apps. They are GET, POST, PUT, DELETE, HEAD, OPTIONS, PATCH. Each of these verbs has associated HTTP Method Attributes defined in  Microsoft.AspNetCore.Mvc.Routing namespace.  

You can apply these attributes to the Controller action methods. When the client sends the request using the specific verb, the routing engine looks for the controller action with that specific attribute and invokes it. 

The HTTP Attributes allows us to define two methods with the same name but responding to different HTTP verbs.

The best example is Edit method. One Edit method responds to a Get Request and renders the Edit Form. The Other method responds to the Post request and updates the database. 

The Code snippet is as shown below. 

Passing routing values in HTTP action verbs

In the Tutorial on Attribute routing, we showed you how to configure the route in the Route Attribute. Instead, you can use the HTTP Action verbs to do the same.

HTTP Attributes

The following is the list of HTTP Attributes.

HttpGet

The HttpGet attribute restricts the Action method to the HTTP request that uses the Get verb. The query strings are automatically appended as the Parameters. The HttpGet is used to retrieve a resource from the Server

HttpPost

The HttpPost attribute restricts the Action method to the HTTP request that uses the Post Verb. The Post verb is used to create a new record.

HttpDelete

The HttpDelete attribute restricts the Action method to the HTTP request that uses the Delete Verb.  The Delete verb is used to delete an existing resource. 

HttpPut

This HttpPut attribute restricts the Action method to the HTTP request that uses the Put Verb.  The Put verb is used to update or create a resource.

HttpHead

The HttpHead attribute restricts the Action method to the HTTP request that uses the Head Verb.  The Head verb is used to retrieve the HTTP header information only. This method is identical to GET except that server do not return message body.

HttpOptions

The HttpOptions attribute restricts the Action method to the HTTP request that uses the Options Verb. This method retrieves the information about the communication options supported by the web server.

HttpPatch

The HttpPatch attribute restricts the Action method to the HTTP request that uses the Options Verb. This method is used to full or partial update the resource.

Using Multiple Action Verbs

The AcceptVerbs HTTP Attribute allows the use of multiple action verbs on an Action Method.

Summary

The Action Name, Non-Action and Action Verbs (collectively known as Action Selectors) gives us more control in the URL Matching Process. The Action Verbs or HTTP Attributes can be used when you want to control the Action method based on HTTP request method.

1 thought on “Action Selectors & Action Verbs in ASP.NET Core”

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top