ASP.NET MVC Action Selectors

Action selectors are attributes that are applied on action methods of a controller. It is used to select correct action method to call as per the request. MVC provides the following action selector attributes:

  1. ActionName
  2. ActionVerbs

ActionName

This attribute allows us to specify a different name for the action method. It is useful when we want to call action by different name.

Example

Here, we are using ActionName attribute to apply different name for the index action method. The controller code looks like this:

// MusicStoreController.cs

Now, we need to create a view in the MusicStore folder as same as the ActionName. So, we have created a store.cshtml file that has following code.

// store.cshtml

Output:

The following output is produced when action is called with different name "store".

ASP Action 1

ActionVerbs

ASP.NET MVC provides action verbs that are applied on the action methods and works for HttpRequest methods. There are various ActionVerbs and listed below.

  • HttpPost
  • HttpGet
  • HttpPut
  • HttpDelete
  • HttpOptions
  • HttpPatch

ActionVerbs are name of the http requests that a controller handle. We can use it for selection among the action methods.

Example

In the following example, we are trying to access an index action by get request, which is accessible only for httpPost request. The controller code looks like this:

// MusicStoreController.cs

Following is the Index file for MusicStoreController.

// index.cshtml

Output:

It produces the following output, when the index action is called.

ASP Action 2

It produces the error message, when we make a get request for the store action method.

ASP Action 3




Contact US

Email:[email protected]

MVC Action Selectors
10/30