Action selectors are attributes that can be applied to action methods and are used to influence which action method gets invoked in response to a request. It helps the routing engine to select the correct action method to handle a particular request.
It plays a awfully crucial role once you area unit writing your action ways. These selectors can decide the behavior of methodology invocation supported the changed name given before of the action method. it’s typically wont to alias the name of the action methodology.
There area unit 3 varieties of action selector attributes −

• ActionName
• NonAction
• ActionVerbs

ActionName

This category represents AN attribute that’s used for the name of AN action. It conjointly permits developers to use a distinct action name than the strategy name.
Let’s take a glance at a straightforward example from the last chapter within which we’ve HomeController containing 2 action ways.

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;

namespace MVCDemo.Controllers {
   public class HomeController : Controller{
      // GET: Home
      public string Index(){
         return "This is ASP.Net MVC Filters Programming Tutorial";
      } 
		
      public string GetCurrentTime(){
         return DateTime.Now.ToString("T");
      }
   }
}

Let’s apply the the ActionName selector for GetSystemCurrentTime by writing [ActionName(“SystemCurrentTime”)] above the GetSystemCurrentTime() as shown in the following code.

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;

namespace MVCDemo.Controllers {
   public class HomeController : Controller{
      // GET: Home
      public string Index(){
         return "This is ASP.Net MVC Filters Tutorial";
      }
		
      [ActionName("SystemCurrentTime")]
      public string GetSystemCurrentTime(){
         return DateTime.Now.ToString("T");
      }
   }
}

Now run this application and enter the following URL in the browser http://localhost:62833/Home/SystemCurrentTime, you’ll receive the subsequent output.

You can see that we have used the SystemCurrentTime instead of the original action name, which is GetSystemCurrentTime in the above URL.

NonAction

NonAction is another built-in attribute, which indicates that a public method of a Controller is not an action method. It is used when you want that a method shouldn’t be treated as an action method.
Let’s take a look at a simple example by adding another method in HomeController and also apply the NonAction attribute using the following code.

using MVCDemo.ActionFilters;
using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;

namespace MVCDemo.Controllers {
   public class HomeController : Controller{
      // GET: Home
      public string Index(){
         return "This is ASP.Net MVC Filters Tutorial";
      }
		
      [ActionName("SystemCurrentTime")]
      public string GetSystemCurrentTime(){
         return SystemTimeString();
      }
		
      [NonAction]
      public string SystemTimeString(){
         return "System Time is " + DateTime.Now.ToString("T");
      }
   }
}

The new method SystemTimeString is called from the GetSystemCurrentTime() but you can’t use it as action in URL.
Let’s run this application and specify the following URL http://localhost:62833/Home/SystemCurrentTime in the browser. You will receive the following output.

Let us now check the /SystemTimeString as action in the URL and see what happens.

You can see that it gives ‘404—Not Found’ error.

ActionVerbs

Another selector filter that you can apply is the ActionVerbs attributes. So this restricts the indication of a specific action to specific HttpVerbs. You can define two different action methods with the same name but one action method responds to an HTTP Get request and another action method responds to an HTTP Post request.
MVC framework supports the following ActionVerbs.
• HttpGet
• HttpPost
• HttpPut
• HttpDelete
• HttpOptions
• HttpPatch
Let’s take a look at a simple example in which we will create UserController.

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;

namespace MVCDemo.Controllers {
   public class UserController : Controller{
      // GET: User
      public ActionResult Search(string userName = No name Entered”){
         var input = Server.HtmlEncode(userName);
         return Content(input);
      }
   }
}

Now let’s add another action method with the same name using the following code.

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;

namespace MVCDemo.Controllers {
   public class UserController : Controller{
      // GET: User
      //public ActionResult Index()
      //{
         // return View();
      //}
		
      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 give an error because the MVC framework is unable to figure out which action method should be picked up for the request.
Let us specify the HttpGet ActionVerb with the action you want as response using the following code.

using System;
using System.Collections.Generic;
using System.Linq;

using System.Web;
using System.Web.Mvc;

namespace MVCDemo.Controllers {
   public class UserController : Controller{
      // GET: User
      //public ActionResult Index()
      //{
         // return View();
      //}
		
      public ActionResult Search(string userName){
         var input = Server.HtmlEncode(userName);
         return Content(input);
      }
		
      [HttpGet]
      public ActionResult Search(){
         var input = "User Search action";
         return Content(input);
      }
   }
}

When you run this application, you will get following output.

Leave a Reply

Your email address will not be published. Required fields are marked *