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.
Table of Contents
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.
- Action Name
- Non Action
- 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.
1 2 3 4 5 6 7 | [ActionName("Modify")] public string Edit() { return "Hello from Edit 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.
1 2 3 4 5 6 7 8 9 10 | public class HomeController : Controller { [Route("Home/Modify")] public string Edit() { return "Hello from Edit Method"; } } |
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
1 2 3 4 5 6 7 8 9 10 | public class HomeController : Controller { [NonAction] public string Edit() { return "Hello from Edit Method"; } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [HttpGet] public ActionResult Edit(string id) { //Return the Edit Form return View(); } [HttpPost] public ActionResult Edit(model Model) { //Update the database here } |
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.
1 2 3 4 5 6 7 8 | [HttpGet("")] [HttpGet("Home")] [HttpGet("Home/Index")] public string Index() { return "Hello from Index method of Home Controller"; } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | [HttpGet] public IEnumerable<product> GetAll() { return db.Products.ToList(); } [HttpGet("{id}"] public IActionResult GetById(long id) { var product= db.Products.Where(e => e.ProductID == id); return View(product); } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 | [HttpPost] public IActionResult Create(Product product) { if (product == null) { return BadRequest(); } db.Products.Add(product); db.SaveChanges(); return RedirectToAction("Index"); } |
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.
1 2 3 4 5 6 7 | [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)] public ActionResult AboutUs() { return View(); } |
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.
Really Helping !!! Gud work sir !!