Types Of Action Selectors
MVC 5 has 3 Action Selectors attributes which are:
- ActionName
- ActionVerb
- NonAction
ActionName
A different name is given to an action method than the one that already exists. We use the ActionName action selector in the ASP.NET MVC Application. To understand the need and use of the ActionName selector, we will look at how the ActionName attribute is used to change the action method's name.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace ActionSelector.Controllers {
public class HomeController: Controller {
[ActionName("Name")]
public ActionResult Index() {
ViewBag.Message = "Action Selector’s ActionName";
return View("Index");
}
}
}
Here in the above code, you can see that we changed the name of ActionMethod from "Index" to "Name" using the ActionName selector.
Therefore, the action name is now "Name" and not "Index". Thus, the Action Method will be invoked by the http://localhost/Home/Name request instead of the http://localhost/Home/Index request.
NonAction
Action methods, which are public methods in ASP.NET MVC controllers, can be invoked through URL requests by Default. We need to restrict the public methods of a controller from being invoked via URL requests in some scenarios, i.e., we do not want to invoke the action method using URLs.
It is necessary to decorate the action method of a controller with a Non-Action attribute to restrict access to public methods. Moreover, the Non-Action attribute can identify public controller methods that are not action methods. We use it when a method should not be considered an action method.
You can't invoke your GetName() public method like you invoke your index() Action method. Below is an example.
public class SelectorController: Controller {
public string Index() {
return "Action method of SelectorController";
}
[NonAction]
public Selector GetName(string Name) {
return view();
}
}
Action Verbs
APIs are increasingly using action verbs. Selectors like this are used to ensure that the Action method is controlled based on the HTTP method. In this case, you can specify two Action methods, but one will respond to the HTTP Get request method, and the other will respond to the HTTP Post request method.
MVC supports the following Action Verbs:
- HttpPost: To create a new resource.
-
HttpGet: The information is retrieved from the server. Parameters are added to the query string.
- HttpPut: To update an existing resource.
- HttpDelete: To delete an existing resource.
-
HttpOptions: This is a request for information on what communication options the webserver supports.
- HttpPatch: To fully or partially update the resource.
In MVC Framework, if you do not apply any of the attributes over the method, it considers a GET request method by Default.
Example:
public class SelectorController: Controller {
public ActionResult Index() {
return View();
}
[HttpPost]
public ActionResult PostAction() {
return View("Index");
}
[HttpPut]
public ActionResult PutAction() {
return View("Index");
}
[HttpDelete]
public ActionResult DeleteAction() {
return View("Index");
}
[HttpHead]
public ActionResult HeadAction() {
return View("Index");
}
[HttpOptions]
public ActionResult OptionsAction() {
return View("Index");
}
[HttpPatch]
public ActionResult PatchAction() {
return View("Index");
}
}
As shown below, the AcceptVerbs attribute allows you to apply multiple action verbs.
[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get | HttpVerbs.GetPatch)]
public ActionResult ActionVerbs() {
return RedirectToAction("Index");
}
Frequently Asked Questions
-
What is the difference between an MVC Selector and a Filter in ASP.NET?
In an action selector, the chosen action is determined by the HTTP verb (e.g., send, upload, download, etc.). There are different filters, such as action filters for code to run before or after an action, exception filters for code to run during exceptions, and authentication filters for code related to authentication.
-
What is a partial view in MVC?
Partially rendered views are markup files (.cshtml) that don't include an @page directive but render HTML output within another markup file's output. Partially rendered views are called views in MVC apps or pages in Razor Pages apps.
-
What is the default route in MVC?
A single route is present in the default route table (named as Default). A URL that starts with a controller name is mapped to a controller action, and a URL that begins with a parameter called id is mapped to a parameter named id.
Key-Takeaways
In this blog, we learned that Action Selectors are built-in properties of ASP.NET that can be applied directly to action methods of controllers. They are used to influence controllers' action methods. Additionally, we learned about different types of Action Selectors.
Thanks for reading it out. We hope you enjoyed it.
Recommended Reading:
If you wish to learn more we encourage you to check out the guided path provided by our team for web development.