Action selectors are attributes that can be used for action methods and influence which action method is used to respond to a request. It helps the routing engine to choose the right action method for handling a specific request.

It plays a very important role when you compose your action methods. These selectors determine the behavior of the invocation method based on the changed name given in front of the action procedure. Usually, the name of the action method is alias. Three types of selector attributes exist ?

  1. ActionName
  2. NonAction
  3. ActionVerbs

1 – ActionName

This class is an attribute that is used for an action’s name. It also allows developers to use an action name different from the method name.

Let’s look at a simple example from the last chapter, where we have a HomeController with two methods of action.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCFiltersDemo.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home

        public string Index()
        {
            return "This is ASP.Net MVC Filters Tutorial";
        }

        public string GetCurrentTime()
        {
            return DateTime.Now.ToString("T");
        }
    }
}

Let’s use the GetCurrentTime ActionName selector by writing[ ActionName(“CurrentTime”)] above the GetCurrentTime() as shown in the code below.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCFiltersDemo.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home

        public string Index()
        {
            return "This is ASP.Net MVC Filters Tutorial";
        }
        [ActionName("CurrentTime")]
        public string GetCurrentTime()
        {
            return DateTime.Now.ToString("T");
        }
    }
}

Now execute this application and enter the following URL in the http:/localhost:4626/Home/CurrentTime browser.

You can see that CurrentTime was used. Instead of the original name for the action that is GetCurrentTime in the URL above.

2 – NonAction

NonAction is another integrated attribute that indicates that a Controller’s public method is not an action method. It is used when you want to avoid treatment of a method as an action method.

Let’s look at a simple example by adding another HomeController method and also apply the NonAction attribute with the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCFiltersDemo.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home

        public string Index()
        {
            return "This is ASP.Net MVC Filters Tutorial";
        }
        [ActionName("CurrentTime")]
        public string GetCurrentTime()
        {
            return TimeString();
        }
        [NonAction]
        public string TimeString()
        {
            return "Time is " + DateTime.Now.ToString("T");
        }
    }
}

The new TimeString method is called from the GetCurrentTime(), but ca n’t be used as an URL action. Run this application and specify in your browser the following URL http://localhost:4626/Home/CurrentTime. You will receive the next output.

Now check the /TimeString in the URL and see what’s going on.

3 – ActionVerbs

The ActionVerbs attributes are another selector filter which you can apply. Consequently, this limits the indication of a specific action to specific Hteads.

You can define two different action methods with the same name, but one action method answers an HTTP request and another action method answers an HTTP request.

The following ActionVerbs supports the MVC framework.

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

Let’s look at a simple example by creating an employee controller.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCFiltersDemo.Controllers
{
    public class EmployeeController : Controller
    {
        // GET: Employee
        public ActionResult Search(string name = "No name Entered")
        {
            var input = Server.HtmlEncode(name);
            return Content(input);
        }
    }
}

Let’s look at a simple example by creating an employee controller.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCFiltersDemo.Controllers
{
    public class EmployeeController : Controller
    {
        // GET: Employee
        public ActionResult Search(string name)
        {
            var input = Server.HtmlEncode(name);
            return Content(input);
        }

        public ActionResult Search()
        {
            var input = "Another Search action";
            return Content(input);
        }
    }
}

When you run this application, it will make a mistake because the MVC framework can not determine which action method should be taken for the application.

Let us specify the ActionVerb with the action you want with the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MVCFiltersDemo.Controllers
{
    public class EmployeeController : Controller
    {
        // GET: Employee
        public ActionResult Search(string name)
        {
            var input = Server.HtmlEncode(name);
            return Content(input);
        }

        [HttpGet]
        public ActionResult Search()
        {
            var input = "Another Search action";
            return Content(input);
        }
    }
}

You will receive the following output when you run this application.